43

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?
jwl
  • 10,268
  • 14
  • 53
  • 91

3 Answers3

36

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.

Sani
  • 1,283
  • 11
  • 30
  • 1
    We have found what appears to be an iOS bug (as of v. 6), in which the OS will tell you that the user declined all notifications even when that's not true. This happens on first launch only: If you call [[UIApplication sharedApplication] enabledRemoteNotificationTypes] in didRegisterForRemote..., it says they're all turned off. They're not, and if you send a push notification, the app will get it. And subsequent launches will return the correct result; the problem is that most apps run in the background, so you don't know when or if the second launch happens. – Oscar Oct 30 '12 at 21:05
  • 5
    Are you sure didRegisterForRemoteNotifications gets called even if you've already registered? I'm trying to implement push notification in my app, and registerForRemoteNotifications worked beautifully the first time, but now neither the success or failure callbacks are being executed on subsequent tries. – Bryan Jan 08 '13 at 19:11
  • @Bryan: how do you fix it? – jAckOdE Jun 06 '13 at 15:15
  • Sort of fixed itself. My app was just in a weird state. Reset everything, delete/re-install your app and try again. – Bryan Jun 06 '13 at 19:17
  • 4
    I'm also seeing didRegisterForRemoteNotificationsWithDeviceToken called when I turn on the "Allow Notifications" option in the Settings app. I have "Allow Notifications" turned off, launch the app, DON'T call registerForRemoteNotifications, go back to Settings app, turn on "Allow Notifications", and when I switch back to my app, didRegisterForRemoteNotificationsWithDeviceToken is called. – Marty Nov 25 '14 at 19:10
  • Call registerForRemoteNotificationTypes in didFinishLaunchingWithOptions so you can get the latest device token on app launch. You may also (and probably do) need it called elsewhere. When users login to your app, you probably need that information sent to your backend paired with the device token anyways, so you know what users to send notifications to. That is, unless you are just broadcasting notifications to all users. – duhseekoh Oct 12 '16 at 20:12
  • One question I cannot find the answer to: will `application:didRegisterForRemoteNotificationsWithDeviceToken` fire if the app is in the background and the token is refreshed? Or will it happen only when the user opens the app? – amza Jul 15 '22 at 21:41
13

When the app is first run it will ask the user whether they will allow remote notifications. If they say yes then it will fire didRegisterForRemoteNotificationsWithDeviceToken at that time and every time after it will fire this function when the app is first opened. If they say no then it will not be fired unless they went into settings and allowed notifications on the app.

rooster117
  • 5,502
  • 1
  • 21
  • 19
2

There can be many reasons, check some reasons

  • If you run the app in the simulator, the application:didFailToRegisterForRemoteNotificationsWithError: method will not be called as push notifications are not supported in the simulator using a device token. You can still send push notifications to your simulator using xcrun simctl push <device> com.example.my-app ExamplePush.apns"

  • Check your deice internet connection if not connect it.

For more info check Link

Ilias Karim
  • 4,798
  • 3
  • 38
  • 60
Denny
  • 25
  • 5