if UIApplication.sharedApplication().isRegisteredForRemoteNotifications() == true {
println("Yes, allowed")
println(UIApplication.sharedApplication().isRegisteredForRemoteNotifications())
} else {
//ignore
return
}
When I go to settings to turn off notifications completely and then go back in the app, the app still prints true
, allowed.
I can't seem to make it trigger false, even after an app uninstall/reinstall.
I made an extension for Swift 3
extension UIApplication {
func remoteNotificationsEnabled() -> Bool {
var notificationsEnabled = false
if let userNotificationSettings = currentUserNotificationSettings {
notificationsEnabled = userNotificationSettings.types.contains(.alert)
}
return notificationsEnabled
}
}
And than use it
UIApplication.shared.remoteNotificationsEnabled()
I have my notes about Push Notification in https://github.com/onmyway133/notes/issues/219
🚀 Scenarios
These are the scenarios that you can go through
isRegisteredForRemoteNotifications
- UIApplication.shared.currentUserNotificationSettings
iOS 9.3.2+
🐕 API
As of iOS 8+, the Push Notification API has been split to registerForRemoteNotifications
and registerUserNotificationSettings
.
So when you call registerForRemoteNotifications
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
is calledUIApplication.shared.isRegisteredForRemoteNotifications
returns trueIt means that the app has receive the push token, and is ready to receive push notification. Whether the OS delivers push to your app depends on user notification settings
, which is what user toggles in the Settings
😎 Show me the code
To check if push is enabled (means that user can see the push message)
static var isPushNotificationEnabled: Bool {
guard let settings = UIApplication.shared.currentUserNotificationSettings
else {
return false
}
return UIApplication.shared.isRegisteredForRemoteNotifications
&& !settings.types.isEmpty
}
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