Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isRegisteredForRemoteNotifications returns true even though I disabled it completely

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.

like image 537
TIMEX Avatar asked Apr 22 '15 04:04

TIMEX


2 Answers

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()
like image 127
zeiteisen Avatar answered Oct 15 '22 03:10

zeiteisen


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+

  • Haven't trigger push request dialog: false - none
  • Enable push for app upon permission request: true - alert, badge, sound
  • Turn off push for app: true - none
  • Turn on push, enable only alert: true - alert
  • Uninstall and install again (within 24 hours): false - none
  • Deny upon asking upon permission request: true - none
  • Reenable push after denial: true - alert, badge, sound

🐕 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 called
  • UIApplication.shared.isRegisteredForRemoteNotifications returns true

It 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
  }
like image 36
onmyway133 Avatar answered Oct 15 '22 03:10

onmyway133