Check if iOS notification status is not confirmed

Im prompting my user to enable push notifications after the user logs in to my app. I know how to test if push notifications are enabled or disabled with:

isRegisteredForRemoteNotifications

      

and everything works fine. It returns YES for allowed and NO for unavailable, but I want to figure out how to check Undefined (the user was not prompted to enable push notifications to begin with). Is there a way to test this?

Thanks in advance!

+3


source to share


2 answers


You create Not Determined

yourself.

func registerNotification() {
    // 1.Call register API
    // ...

    // 2.Save a bool value to indicate you've called this method or not.
    let appleSuggestedUserDefaultsKeyPrefix = "com.yourcompany.product-"
    let key = appleSuggestedUserDefaultsKeyPrefix + "didCallRegisterNotificationAPI"
    NSUserDefaults.standardUserDefaults().setBool(true, forKey: key)
}

      

And in the method, didFinishLaunchingWithOptions

you need to check if you called registerNotification()

.



func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    let didCallRegisterNotificationAPI = NSUserDefaults.standardUserDefaults().boolForKey(...)
    if didCallRegisterNotificationAPI {
        // If registered or user denied, call this method will NOT show the alert. 
        // Just the same as you did before.
        registerNotification()
    } else {
        print("registerNotification() has not been called")
    }
}

      

And finally, you can call registerNotification()

directly anywhere, anytime you need, and now the alert is under your control.

+2


source


isRegisteredForRemoteNotifications

is Bool

. No undefined status. You can check that this is reference .

When a user first installs your app, they must either allow or deny push notifications. There is no other option.

However, you may be asking because you can uninstall the app, reinstall it and not ask for permission. This is because the permission is remembered.

Resetting Push Notification Allow Alerts on iOS The first time a push-enabled app registers for push notifications, iOS asks the user if they want to receive notifications for that app. Once the user has responded to this warning, it will not be re-submitted unless the device is restored or the app is uninstalled for at least one day.

If you want to simulate the first launch of your application, you can leave the application uninstalled for a day. You can achieve the latter without waiting for a day by following these steps:

Delete the app from your device. Turn off the device completely and turn it on again. Go to Settings> General> Date & Time and set the date to a day or more. Turn off the device completely and turn it on again.



Link

Related question: When I uninstall my iOS app state notification app, it remains

EDIT:

You can only use it isRegisteredForRemoteNotifications

to check that they are simply not registered, because you declined or because you never tried to register.

However, as long as you are trying to register in valid order (valid certificates and profiles, etc.) and the user refuses, your application will call, register, but with nil UIUserNotificationSettings

:

 func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {

    if notificationSettings.types == nil {
        println("You didn't allow notifcations")
    }

}

      

+1


source







All Articles