No response from +UIApplication registerForRemoteNotifications after re-registering

Push notifications worked now for month in my app without any problem. But having my app on an iOS 10 device causes problems. When the app is initially installed I can successfully enable push notifications with

UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
                              completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                  if (granted) {
                                      [[UIApplication sharedApplication] registerForRemoteNotifications];
                                  }
                              }];


and application:didRegisterForRemoteNotificationsWithDeviceToken: will be successfully called. Receiving push notifications works. But when I unregister from push notifications with

[[UIApplication sharedApplication] unregisterForRemoteNotifications];

and try to re-register with the same code at the top again, neither application:didRegisterForRemoteNotificationsWithDeviceToken:, nor application:didFailToRegisterForRemoteNotificationsWithError: will get called. Closing the app and re-open doesn't help. Only uninstalling and reinstalling the app 'solves' the problem and my app is again correctly registering for push and receving the token.

I even checked to see if something is failing with the call to the apple servers when registering, but there isn't even any request after calling registerForRemoteNotifications for the second time.

What an I doind wrong? Is this an iOS 10 bug?

Accepted Reply

I found the solution. The problem seems to be that iOS 10 prevents registering for push notification with Apple in a manner of time (24h?) after unregistering. The Apple Docs regarding unregistering say:

You should call this method in rare circumstances only, such as when a new version of the app removes support for all types of remote notifications. Users can temporarily prevent apps from receiving remote notifications through the Notifications section of the Settings app. Apps unregistered through this method can always re-register.

https://developer.apple.com/reference/uikit/uiapplication/1623093-unregisterforremotenotifications?language=objc

So, just don't call unregisterForRemoteNotifications as long as you really need to do it (see Apple Doc).

Replies

Hi KaiMaschke,


I can also confirm the same behaviour you are experiencing. This definitely seems like an Apple bug.


After calling:


[[UIApplication sharedApplication] unregisterForRemoteNotifications]


subsequent attempts to use:


[[UIApplication sharedApplication] registerForRemoteNotifications]


do not invoke application:didRegisterForRemoteNotificationsWithDeviceToken:, nor application:didFailToRegisterForRemoteNotificationsWithError.


Also, before making this attempt to re-register, I checked:


[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]


and it returned False. Therefore, there is no reason why registerForRemoteNotifications shouldn't invoke one of these callbacks in this state.

I had a deep look into the logs. I think I kind of have a trail:

The first time when calling requestAuthorizationWithOptions:completionHandler:, some lines later I found this log entry


Sep 15 16:29:16 Kais-iPhone SpringBoard(UserNotificationsServer)[549] <Notice>: [com.company.app] Enable notifications: 1 [authorizationOptions: 7]


The second time I am calling requestAuthorizationWithOptions:completionHandler:, I saw this line immediately following:


Sep 15 16:29:26 Kais-iPhone SpringBoard(UserNotificationsServer)[549] <Notice>: [com.company.app] Enable notifications: 0 [authorizationOptions: 7]


For me it looks like that the UserNotificationsServer is not correctly handling the authorization of the user regarding notifications. In the completionHandler of requestAuthorizationWithOptions:completionHandler: I am always correctly receiving a 'granted' = YES. But my assumption is now, that Apple isn't handling this correct internally when doing the registerForRemoteNotifications call.

I found the solution. The problem seems to be that iOS 10 prevents registering for push notification with Apple in a manner of time (24h?) after unregistering. The Apple Docs regarding unregistering say:

You should call this method in rare circumstances only, such as when a new version of the app removes support for all types of remote notifications. Users can temporarily prevent apps from receiving remote notifications through the Notifications section of the Settings app. Apps unregistered through this method can always re-register.

https://developer.apple.com/reference/uikit/uiapplication/1623093-unregisterforremotenotifications?language=objc

So, just don't call unregisterForRemoteNotifications as long as you really need to do it (see Apple Doc).

Hi KaiMaschke,


What may I do if a user logout from my application (actually I call unregisterForRemoteNotifications) ?


With iOs 10, user who logout then login again cannot receive notification ...

I found best solution is mark the app capability "background fetch". That will make "application:didRegisterForRemoteNotificationsWithDeviceToken" be called after call "registerForRemoteNotifications", doesn't matter how many times did you "unregisterForRemoteNotifications".

Thanks,

Edu

Hi ieduardogf,

I tried with "background fetch" but still not work for me. I using same functional as registerForRemoteNotifications / unregisterForRemoteNotifications many times.

This advice was really helpful.Thank you so much!!