4

We know that when a user is prompted with the system dialog in an app asking for Push Notification permission, if he clicks "YES" then application:didRegisterForRemoteNotificationsWithDeviceToken: will be called, if he clicks "NO" then application:didFailToRegisterForRemoteNotificationsWithError: will be called.

What if the user clicks "NO", then later on goes to Settings and manually turns on push notifications? Upon returning to the app, will a certain delegate method be triggered? I would like to execute a block of code as soon as the user turns on push notifications in Settings, what is the best way to detect that, without trying to register again every time on applicationDidBecomeActive?

2
  • AFAIK there isn't any continuous service that you can use to detect this change in real time. There is a good discussion here - stackoverflow.com/questions/1535403/… .Best way is to detect the enabled types for PUSH when required and request permission when you really need to seek user permission for allowing PUSH. Also what i recall is that if user denies this prompt three times in a row then you can not show this prompt un till the next install (Please verify for exact number of times and if this behavior is still valid.) Apr 6, 2015 at 22:50
  • Thank you, that link is very helpful. As for your question of prompting the dialog, here is Apple's answer: link @RahulSharma
    – SeaJelly
    Apr 6, 2015 at 23:02

2 Answers 2

2

If the user denies your request for notifications then didFailToRegisterForRemoteNotificationsWithError: is not called, because registration didn't fail - it wasn't even attempted.

If the user changes the permissions in the settings app then you will receive a call to didRegisterForRemoteNotificationsWithDeviceToken: either the next time your app is launched or when your app returns to the foreground if it is in the background.

The successful registration of remote notifications doesn't mean that you can actually notify the user - for that you need to check the value passed to didRegisterUserNotificationSettings:, however if all you are interested in is the ability to receive background push notifications then didRegisterForRemoteNotificationsWithDeviceToken: may be sufficient

6
  • 2
    "If the user changes the permissions in the settings app then you will receive a call to didRegisterForRemoteNotificationsWithDeviceToken: either the next time your app is launched or when your app returns to the foreground if it is in the background." This is not true, it didn't get triggered when my app goes from background to foreground after changing Settings. The only way I can get it triggered for now is 1) initial dialog asking for permission; 2) I manually request push token again in applicationDidBecomeActive @Paulw11
    – SeaJelly
    Apr 6, 2015 at 23:01
  • I ran that test on my app before posting my answer and the delegate method was called. My steps were - Run the app, click "deny". Go into settings and enable the notification for the app. Return to the app. Immediately a breakpoint triggered in didRegisterForRemoteNotificationsWithDeviceToken. You should always call registerForRemoteNotifications every time your app is launched regardless of any previous successful registrations
    – Paulw11
    Apr 6, 2015 at 23:04
  • Ah, so your suggestion is to always call registerForRemoteNotifications every time your app is launched regardless of any previous successful registrations. I think that'll work, but I was wondering whether it's possible to not do this every time on app resume? After all push token very likely stays the same most of the time
    – SeaJelly
    Apr 6, 2015 at 23:07
  • But it may change, the user may change the settings. You need to register on every launch - not on every resume (ie. not from didEnterForeground, didBecomeActive) simply from didFinishLaunching...
    – Paulw11
    Apr 6, 2015 at 23:09
  • I have a method that executes every time on app resume that will need to send the most up-to-date push token to the server and show a screen according to the server callback, which is why I need to register every time on app resume
    – SeaJelly
    Apr 6, 2015 at 23:18
0

once user click on allow or don't allow on push notification popup, below delegate will call for sure on both options.

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.