FIRInstanceID令牌在第一次应用启动时为null,第二次是OK

时间:2016-12-07 02:27:34

标签: ios objective-c firebase firebase-cloud-messaging

我正在iOS应用上实施Firebase推送通知。

didFinishLaunchingWithOptions,我致电[FIRApp configure]。它在应用程序第一次启动时返回nill标记,但如果我再次运行该应用程序,它将返回一个有效的工作标记。

 NSString *refreshedToken = [[FIRInstanceID instanceID] token];

第一次为空。

以下是application didFinishLaunchingWithOptions:中的代码:

- (void)handleFCMregister
{
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
        UIUserNotificationType allNotificationTypes =
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings =
        [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else {
        // iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
        UNAuthorizationOptions authOptions =
        UNAuthorizationOptionAlert
        | UNAuthorizationOptionSound
        | UNAuthorizationOptionBadge;
        [[UNUserNotificationCenter currentNotificationCenter]
         requestAuthorizationWithOptions:authOptions
         completionHandler:^(BOOL granted, NSError * _Nullable error) {
         }
         ];

        // For iOS 10 display notification (sent via APNS)
        [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
        // For iOS 10 data message (sent via FCM)
        [[FIRMessaging messaging] setRemoteMessageDelegate:self];
#endif
    }

    [[UIApplication sharedApplication] registerForRemoteNotifications];

    [FIRApp configure];

    NSString *refreshedToken = [[FIRInstanceID instanceID] token];        
    DLog(@"refreshedToken ::%@", refreshedToken);
}

如何在应用首次启动时获取有效令牌?什么似乎是错的?感谢。

0 个答案:

没有答案