didRegisterForRemoteNotificationsWithDeviceToken called twice?

寵の児 提交于 2020-01-01 08:19:05

问题


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.


回答1:


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.




回答2:


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.




回答3:


Check the Application State inside the function didReceiveRegistrationToken

let state = UIApplication.shared.applicationState
        if state == .background || state == .inactive {
            print("Background")
        } else if state == .active {
            print("foreground")
        }


来源:https://stackoverflow.com/questions/18589755/didregisterforremotenotificationswithdevicetoken-called-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!