Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didRegisterForRemoteNotificationsWithDeviceToken called twice?

Somewhat similar to When is didRegisterForRemoteNotificationsWithDeviceToken called?.

When the user first installed the app and it prompts whether to accept notification, will didRegisterForRemoteNotificationsWithDeviceToken be called if the user accepts it?

I'm currently calling registerForRemoteNotificationTypes after the user successfully signs in or creates an account.

In subsequently launches, the didRegisterForRemoteNotificationsWithDeviceToken delegate is called even without calling registerForRemoteNotificationTypes in AppDelegate.

If I follow the docs and example code:

By requesting the device token and passing it to the provider every time your application launches, you help to ensure that the provider has the current token for the device.

- (void)applicationDidFinishLaunching:(UIApplication *)app {

   // other setup tasks here....
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:...
}

But by requesting the token when the app launches, the delegate will be called twice.

A couple of questions and clarity needed:

  1. For the initial prompt to happen, a call for registerForRemoteNotificationTypes must be made for the prompt to appear? But, the delegate will be called twice if I call this manually in AppDelegate. Is it suppose be that way?

  2. If the user accepts the initial prompt, will didRegisterForRemoteNotificationsWithDeviceToken be called automatically? Or must we invoke registerForRemoteNotificationTypes in AppDelegate? But then the delegate will be called twice for future launches?

  3. If the user denies and later accept it via Setting, what happens?

UPDATE

0. For the initial prompt to happen, a call for registerForRemoteNotificationTypes must be made for the prompt to appear? But, the delegate will be called twice if I call this manually in AppDelegate. Is it suppose be that way? This isn't true. Found out that there were actually 2 registerForRemoteNotiicationTypes being made in AppDelegate.

like image 867
Nora Olsen Avatar asked Sep 03 '13 10:09

Nora Olsen


2 Answers

If the user accepts the initial prompt, will didRegisterForRemoteNotificationsWithDeviceToken be called automatically? No, Your remote notification registration process starts when 'allow access' is clicked, but didRegisterForRemoteNotificationsWithDeviceToken method is called only on successful APNS registration.

If your application has previously registered, calling registerForRemoteNotificationTypes: results in the operating system passing the device token to the delegate immediately without incurring additional overhead

You can determine whether an application is launched as a result of the user tapping the action button or whether the notification was delivered to the already-running application by examining the application state. In the delegate’s implementation of the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method, get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.

like image 175
thatzprem Avatar answered Nov 03 '22 01:11

thatzprem


Nope. this method only called once when app launch . if it is called twice then it will be called from your code. Try to see that if you are putting code like

[application registerUserNotificationSettings:mySettings];
[application registerForRemoteNotifications];

in didFinishLaunchingWithOptions

then in go to

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

is that you might be calling [application registerForRemoteNotifications];

so remove this code.

like image 2
Raj Oriya Avatar answered Nov 03 '22 02:11

Raj Oriya