I have push notifications in my app. Whenever the app is launched, I would like to check whether the user has enabled push notification for my application.
I do it this way :
let notificationType = UIApplication.sharedApplication().currentUserNotificationSettings()!.types
if notificationType == UIUserNotificationType.None {
print("OFF")
} else {
print("ON")
}
If push notifications are disabled by the user, is there any way to activate this from my app?
Or is there any alternative to send user to the push notification settings (Settings - Notifications - AppName)?
Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.
If "Push Notifications" are part of the Enabled Capabilities for that profile, then you can send Push Notifications to your users. If Push Notifications does not appear, then you will need to create a new distribution certificate and provisioning profile, upload it to the mag+ Publish portal, and rebuild your app.
There is no way to change settings from the app. But you can lead user to application specific system settings using this code.
extension UIApplication {
class func openAppSettings() {
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}
}
Updated for Swift 3.0
extension UIApplication {
class func openAppSettings() {
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
}
}
Updated for iOS 10+ & Swift 5+
extension UIApplication {
@objc class func openAppSettings() {
shared.open(URL(string: openSettingsURLString)!,
options: [:],
completionHandler: nil)
}
}
Checking if push notifications have been enabled for your app has changed dramatically for Swift 3. Use this instead of the above examples if you are using Swift 3.
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in
if(settings.authorizationStatus == .authorized)
{
print("Push authorized")
}
else
{
print("Push not authorized")
}
}
Here is Apple's documentation on how to further refine your check: https://developer.apple.com/reference/usernotifications/unnotificationsettings/1648391-authorizationstatus
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With