Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

APNS didRegisterForRemoteNotifications called before user allows notifications on iOS

I'm working on some iOS apps, all under the same publisher, that all have push notifications enabled. When I call registerForRemoteNotificationTypes, I get the user prompt to allow or disallow push notifications, but I application:didRegisterForRemoteNotificationsWithDeviceToken: gets called with token data before the user chooses an option. And it gets called again when they press OK. Is this normal?

Also of note: multiple apps appear to get the same token when running on the same device.

like image 653
Tom Hamming Avatar asked Mar 19 '13 20:03

Tom Hamming


2 Answers

This is from Apple docs.

When you send this message, the device initiates the registration process with Apple Push Service. If it succeeds, the application delegate receives a device token in the application:didRegisterForRemoteNotificationsWithDeviceToken: method; if registration doesn’t succeed, the delegate is informed via the application:didFailToRegisterForRemoteNotificationsWithError: method. If the application delegate receives a device token, it should connect with its provider and pass it the token.

AND

The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

I think what you are observing is normal. There is no mention that application:didRegisterForRemoteNotificationsWithDeviceToken will only be called if user grants permission. I think you can optimize it by caching device token in NSUserDefaults and in this method check if the new token not equal to cached token.

like image 181
msk Avatar answered Sep 20 '22 15:09

msk


It's an old question, but I've just encountered this issue and it seems to be related to remote-notification background mode. application:didRegisterForRemoteNotificationsWithDeviceToken is called on my iPhone before accepting push notification permission only when this background mode is turned on.

like image 38
mihai1990 Avatar answered Sep 19 '22 15:09

mihai1990