When is didRegisterForRemoteNotificationsWithDeviceToken called?

jwl picture jwl · Jun 11, 2012 · Viewed 35.4k times · Source

There are a lot of questions about didRegisterForRemoteNotificationsWithDeviceToken but they all sidestep a very direct question which I cannot seem to find an exact answer to.

For an app which is properly set up for notifications in all other ways and has proper network connectivity: when is didRegisterForRemoteNotificationsWithDeviceToken called? Some possible choices might be:

  1. Every time the app starts
  2. Only after the initial prompt to the user to accept push notifications
  3. Something else?

Answer

Abd Sani Abd Jalal picture Abd Sani Abd Jalal · Jun 12, 2012

The application delegate will call the method upon successful registration of remote notification after you call this method in your UIApplication:

(void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types

According to: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html

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.

Now, to elaborate further, normally an app will call the registerForRemoteNotificationTypes in your didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in your application delegate. And therefore, the application:didRegisterForRemoteNotificationsWithDeviceToken is then usually called moments after the launch of the application.

Edit: The application:didRegisterForRemoteNotificationsWithDeviceToken still gets called for subsequents registration after the first.