didRegisterForRemoteNotificationsWithDeviceToken called twice?

后端 未结 3 1915
南方客
南方客 2021-01-04 03:36

Somewhat similar to When is didRegisterForRemoteNotificationsWithDeviceToken called?.

When the user first installed the app and it prompts whether t

相关标签:
3条回答
  • 2021-01-04 04:20

    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")
            }
    
    0 讨论(0)
  • 2021-01-04 04:24

    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.

    0 讨论(0)
  • 2021-01-04 04:27

    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.

    0 讨论(0)
提交回复
热议问题