在真实设备上无法获取 FCM 令牌,但在模拟器上可以获取

4

我已经在我的iOS应用中集成了Firebase云消息传递功能,但没有使用cocoa pods。 Firebase分析工作正常,但在真实设备上收不到FCM令牌,只能在模拟器上接收到。在真实设备上,我一直收到以下错误:

Failed to fetch default token Error Domain=com.firebase.iid Code=501 "(null)"

  1. 我已在Firebase上上传了用于开发和生产的.p12证书。
  2. 我已检查了我的应用程序的bundle ID以及Firebase控制台上的bundle ID。
  3. 我已启用了我的App ID的推送通知功能。

这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [FIRApp configure];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)  name:kFIRInstanceIDTokenRefreshNotification object:nil];
}


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

    // For iOS 10 display notification (sent via APNS)
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    // For iOS 10 data message (sent via FCM)
    [FIRMessaging messaging].remoteMessageDelegate = self;

    [application registerForRemoteNotifications];

}

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

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

}

- (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.
}

- (void)connectToFcm {
    // Won't connect since there is no token
    if (![[FIRInstanceID instanceID] token]) {
        return;
    }

    // Disconnect previous FCM connection if it exists.
    [[FIRMessaging messaging] disconnect];

    [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Unable to connect to FCM. %@", error);
        } else {
            NSLog(@"Connected to FCM. FCM token - %@", [[FIRInstanceID instanceID] token] );
        }
    }];
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBSDKAppEvents activateApp];

    [self connectToFcm];

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

    [[FIRMessaging messaging] disconnect];
    NSLog(@"Disconnected from FCM");

}

请帮忙。


正确格式化您的代码,可以增加获得答案的机会。 - Rajat
谢谢Rajat。你能帮我吗? - Swati
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Rajat
我已经做过了。你知道为什么在模拟器上可以获取到token,但在设备上却获取不到吗? - Swati
3个回答

3

我解决了自己的问题。设备的日期和时间不正确。当我将其更改为当前日期和时间时,Firebase开始给我提供FCM令牌并正常连接。我还从Firebase通知控制台检查了推送通知。它的工作方式比我想象的要好得多。


0
可能问题是由于版本问题引起的。实际上,我也遇到了同样的问题。我在模拟器上得到了FCM令牌,但在设备上没有得到。原因是在didFinishLaunchingWithOptions中注册了用户通知设置...我们必须检查设备和模拟器的版本...如果两者不同...请检查当前设备版本的“[[UIApplication sharedApplication] registerUserNotificationSettings:settings]”条件...因为我在didFinishLaunchingWithOptions中限制了>=10个版本。

0

Swati 上面的回答指引了我找到这个解决方案。我已经尝试了多次重启设备以及多次重新编写代码,但是都没有成功。Swati 说她的设备时间不对,在我的情况下,我使用 NordVPN。我暂停了服务,运行了项目,然后 FCMToken 被打印并上传到了我的数据库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接