I am building an app that targets iOS7 and iOS8. I ask the user for permission to send push notifications. But for some reason, neither iOS7 nor iOS8 ever calls my application:didFailToRegisterForRemoteNotificationsWithError
handler if I click "No" when asked for push permission.
My question: how do I know, on both iOS7 and iOS8, when the user has dismissed the request for push notifications--and how do I know if they denied the request?
I've looked at a bunch of StackOverflow answers and have implemented what they suggest, but it's not working as documented:
application:didRegisterForRemoteNotificationsWithDeviceToken:
handler. So I can tell that they approved it. But if the user denies the request then I don't get a callback to application:didFailToRegisterForRemoteNotificationsWithError
. This seems like a bug, and I can't tell that the user denied the request.application:didRegisterUserNotificationSettings
handler. I can look at the notificationSettings
parameter to see if the user approved or denied my request, so that's handy. isRegisteredForRemoteNotifications()
(e.g., when the app becomes active later on), it always returns true--even if the user had denied the request. So I get a false positive. This seems like a bug and I see that others have noticed this as well.let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
, I can check settings.types
to see if alerts are allowed. So for iOS8, it looks like I'm all set.I'm using an NSUserDefaults
boolean to keep track of whether I've already asked the user to grant permission.
I am using hardware devices for testing (iPhone 4S with iOS7 and iPhone 5 with iOS8), not a simulator.
I am resetting my device after each test, to make it show the request alert again.
Here's how I register for push notifications. The if
branch is taken on iOS8 and the else
branch is taken on iOS7:
let application = UIApplication.sharedApplication()
if (application.respondsToSelector("registerUserNotificationSettings:")) {
let settings = UIUserNotificationSettings(forTypes: .Badge | .Alert | .Sound,
categories: nil )
application.registerUserNotificationSettings(settings)
} else {
application.registerForRemoteNotificationTypes(.Badge | .Alert | .Sound)
}
(In iOS8, when application:didRegisterUserNotificationSettings:
is called, I then call application.registerForRemoteNotifications()
).
Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.
Let's start with Android. The Android OS is designed to listen for push messages and upon receiving one, wake up the appropriate Android app to handle the push message, regardless of whether the app is closed or not.
Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.
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:
didRegisterForRemoteNotificationsWithDeviceToken
is calledenabledRemoteNotificationTypes
on the UIApplication
objectIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With