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

nkcskrwz  于 2023-06-07  发布在  iOS
关注(0)|答案(3)|浏览(474)

我已经在我的iOS应用程序中集成了Firebase云消息传递,而没有可可Pod。Firebase Analytics工作正常。但FCM令牌在模拟器上接收,而不是在真实的设备上接收。在真实的设备上,我不断收到错误
无法获取默认令牌错误域=com.firebase.iid代码=501“(null)”
1.我已经上传了.p12证书用于Firebase的开发和生产
1.我已经检查了我的应用程序和Firebase控制台上的捆绑包ID
1.我在我的应用程序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");

}

请帮帮我

csga3l58

csga3l581#

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

cgfeq70w

cgfeq70w2#

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

m4pnthwp

m4pnthwp3#

Swati上面的回答给我指出了一个解决方案。我已经试过几次重新启动设备,更不用说对我的代码进行各种重写了。Swati说她的设备的日期是错误的,在我的情况下,我使用NordVPN。我暂停服务,运行项目,FCMToken被打印并上传到我的数据库。

相关问题