Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify "Not Determined" case for Local Notifications settings

As of iOS8 you to need to register and prompt the user for the use of Local Notifications. Therefore I'd like to implement a way to double check these permissions.

How can I check for the case of Local Notifications settings being not determined/not set yet? So far I only know how to check for Local Notifications being granted or denied, like this …

    var currentStatus = UIApplication.sharedApplication().currentUserNotificationSettings()
    var requiredStatus:UIUserNotificationType = UIUserNotificationType.Alert

    if currentStatus.types == requiredStatus {
     … //GRANTED
     } else {
     … //DENIED
     }

The problem using this is I also get a Denied if there is nothing set so far. How can I differentiate all 3 cases?

  • Granted (Notification, Alert Type)
  • Denied (Notification, Alert Type)
  • Undefined/Not set yet (therefore local notification app settings not created yet)

As an alternative solution it would be helpful to have a comparable delegate method to CoreLocation's authorization didChangeAuthorizationStatus in order to react on the user's selection on the permissions alert. Is there something like this in order to get the state of user interaction with the privacy alert for local notifications?

like image 295
Bernd Avatar asked Oct 22 '14 23:10

Bernd


2 Answers

The solution I implemented :

In app delegate, I detect when didRegisterUserNotificationSettings is shot. And I save in userdefaults a bool to true :

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "notificationsDeterminedKey")
        NSUserDefaults.standardUserDefaults().synchronize()
}

And when I need to know the status :

if NSUserDefaults.standardUserDefaults().boolForKey("notificationsDeterminedKey") {
    let grantedSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
    if grantedSettings.types == UIUserNotificationType.None {
        // Denied
    } else {
        // Granted
} else {
    // Not Determined
}
like image 134
Ambroise Collon Avatar answered Oct 21 '22 08:10

Ambroise Collon


I just found an appropriate UIApplication delegate method that helps to solve this:

- (void)application:(UIApplication *)application
    didRegisterUserNotificationSettings:
    (UIUserNotificationSettings *)notificationSettings {

You can find more details on this in WWDC14 Session 713 "What’s New in iOS Notifications".

like image 41
Bernd Avatar answered Oct 21 '22 08:10

Bernd