令牌在首次使用后未注册

时间:2016-09-28 09:15:27

标签: ios firebase firebase-cloud-messaging

我在iOS应用程序中使用了ios-quickstart示例。

当我通过firebase控制台向我的设备发送推送通知时,第一个发送并标记为成功,但从未到达。

在第一个之后发送的任何通知都会返回以下响应。

{
  "multicast_id": 8642270861911984600,
  "success": 0,
  "failure": 1,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    }
  ]
}

因此,由于某种原因,FIRMessaging令牌未注册,但我无法弄清楚原因。

我已经尝试过没有成功:

  • 使用apple developer console创建新证书
  • 删除并重新添加了所有代码
  • 已启用和已停用方法调整(FirebaseAppDelegateProxyEnabled

修改

为了得到上述响应,我使用PostMan和FCM REST API。 使用REST API具有相同的结果,第一次推送提供正常响应,之后的任何推送都会产生NotRegistered错误

使用控制台时,我得到以下内容: Firebase Console Example

除了放置在registerForPushNotifications:中调用的-application: didFinishLaunchingWithOptions:函数之外,app委托与quickstart-ios示例完全相同:

    -(void) registerForPushNotifications:(UIApplication*) application
    {
        NSLog(@"%s",__PRETTY_FUNCTION__);
        // Register for remote notifications
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
            // iOS 7.1 or earlier. Disable the deprecation warnings.
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
            UIRemoteNotificationType allNotificationTypes =
            (UIRemoteNotificationTypeSound |
             UIRemoteNotificationTypeAlert |
             UIRemoteNotificationTypeBadge);
            [application registerForRemoteNotificationTypes:allNotificationTypes];
    #pragma clang diagnostic pop
        } else {
            // iOS 8 or later
            // [START register_for_notifications]
            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];
            // [END register_for_notifications]
        }
    }

    -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
    {
        NSLog(@"%s",__PRETTY_FUNCTION__);
        if (notificationSettings != UIUserNotificationTypeNone)
        {

            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
    }

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        NSLog(@"%s",__PRETTY_FUNCTION__);
    //    //source: http://stackoverflow.com/a/9372848/5309449
        const unsigned *tokenBytes = [deviceToken bytes];
        NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                              ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                              ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                              ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
        NSLog(@"hexToken: %@",hexToken);


        //[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];



    }

    -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
    {
        NSLog(@"%s",__PRETTY_FUNCTION__);
        NSLog(@"Failed to register: %@",error);
    }


    // [START refresh_token]
    - (void)tokenRefreshNotification:(NSNotification *)notification {
        // Note that this callback will be fired everytime a new token is generated, including the first
        // time. So if you need to retrieve the token as soon as it is available this is where that
        // should be done.
        NSString *refreshedToken = [[FIRInstanceID instanceID] token];
        NSLog(@"InstanceID token: %@", refreshedToken);

        // Connect to FCM since connection may have failed when attempted before having a token.
        [self connectToFcm];

        // TODO: If necessary send token to application server.

        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:refreshedToken forKey:@"FIRMessagingToken"];
    }
    // [END refresh_token]

    // [START connect_to_fcm]
    - (void)connectToFcm {
        [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"Unable to connect to FCM. %@", error);
            } else {
                NSLog(@"Connected to FCM.");
            }
        }];
    }
    // [END connect_to_fcm]

    // [START receive_message]
    // To receive notifications for iOS 9 and below.
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

        // Print message ID.
        NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

        // Print full message.
        NSLog(@"%@", userInfo);
    }
    // [END receive_message]

    // [START ios_10_message_handling]
    // Receive displayed notifications for iOS 10 devices.
    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center
           willPresentNotification:(UNNotification *)notification
             withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
        // Print message ID.
        NSDictionary *userInfo = notification.request.content.userInfo;
        NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

        // Print full message.
        NSLog(@"%@", userInfo);
    }

    // Receive data message on iOS 10 devices.
    - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
        // Print full message
        NSLog(@"%@", [remoteMessage appData]);
    }
    #endif
    // [END ios_10_message_handling]

我发现其他有趣的事情是,在显示苹果通知权限提醒时,我可以发送通知,我看到它们到达我的调试控制台。

我接受通知权限后,他们不再到达...

1 个答案:

答案 0 :(得分:1)

权利似乎存在问题。

我最近也升级到XCode 8,可能已经破坏了一些东西。

有一个&#39;修复此问题&#39;这个屏幕上的按钮:

enter image description here