iOS7 and iOS8: how to detect when user said No to a request for push notifications
I was trying to find a way around this same issue.
When the push notification permission UIAlert is shown, it is shown from outside of my app. Once the user has selected "Don't Allow" or "OK", my app is becoming active again.
I have a view controller that I'm presenting before prompting the user for Push permissions. In this view controller I listen for the UIApplicationDidBecomeActiveNotification
, and then dismiss my view controller.
This has been working pretty well, whereas every other solution I've seen hasn't worked for me at all.
Your didFailToRegisterForRemoteNotificationsWithError
method isn't called, because the registration didn't fail - it was never even attempted because the user denied the request.
On iOS7 you can do a couple of things:
- Assume that remote notifications aren't available until
didRegisterForRemoteNotificationsWithDeviceToken
is called - Check the value
enabledRemoteNotificationTypes
on theUIApplication
object
Assuming that you're compiling against iOS8 or newer, you can use the following delegate method:
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
print("Notifications status: \(notificationSettings)")
}
then on your didFinishLaunching (or where is appropriate for your needs) you must invoke:
let app = UIApplication.sharedApplication()
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
app.registerUserNotificationSettings(settings)
app.registerForRemoteNotifications()
At this point, your users will be prompted with a typical Allow/Don't Allow prompt message. Regardless of your user's choice you will get a call to the method above, allowing for a fine grain configuration of your app. The result will be something like:
Notification Status: <UIUserNotificationSettings: 0x7fa261701da0; types: (none);>
Notification Status: <UIUserNotificationSettings: 0x7ff032e21dd0; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);>
In the first case, you will notice that the user denied the notifications. The second one is about the allow option.