2

There are several questions that come close to this, but I haven't found anything that's exactly right:

This does not related to remote notifications, I am only using local notifications.


I'd like to determine the current state of the local notifications permissions, so that I can show one of three interfaces:

  • Notifications are enabled
  • Notifications are not enabled, here's a button to enable them (which would trigger the standard dialog)
  • Notifications have been denied (so the dialog cannot be displayed again), here's a link to Settings.app, so they can be enabled there.

It's easy to figure out if notifications are enabled with currentUserNotificationSettings(), but I can't figure out how to determine between "not enabled because we haven't asked" and "not enabled because the user said no".

  • I believe that only way to detect it, is to track this on your own, i.e. if you asked about notification save some flag, for example some bool in NSUserDefaults. – shpasta Jan 28 '16 at 20:52

1 Answers1

0

As @shpasta stated, you have to track this using NSUserDefaults. Using NSUserDefaults is very easy.

After calling registerUserNotificationSettings:, add this code:

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "didRegister")

Then, wherever you want to check for your current state of local notifications, use this:

if UIApplication.sharedApplication().currentUserNotificationSettings().types == TYPES_YOU_NEED {
    // Code for enabled notifications
} else if NSUserDefaults.standardUserDefaults().boolForKey("didRegister") {
    // Code for denied notifications
} else if !(NSUserDefaults.standardUserDefaults().boolForKey("didRegister")) {
    // Code for non-enabled notifications, can display dialog
}

The only caveat with the above implementation is if the user only allowed some of the types you need, not all of them. You would check for that in the first condition, but I didn't add it in because I don't know what you want for that particular situation.

tktsubota
  • 9,371
  • 3
  • 32
  • 40