无法在真实设备上获取FCM令牌,但正在模拟器上 [英] Unable to get FCM token on real device but getting on simulator

查看:184
本文介绍了无法在真实设备上获取FCM令牌,但正在模拟器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在没有可可豆荚的iOS应用程序中集成了Firebase云消息传递.Firebase分析工作正常.但是FCM令牌是在模拟器上收到的,而不是在真实设备上收到的.在真实设备上,我不断收到错误

I have integrated Firebase cloud messaging in my iOS application without cocoa pods. Firebase analytics is working fine. But FCM token is received on simulator but not real device. On real device I keep getting error

无法获取默认令牌错误Domain = com.firebase.iid代码= 501(空)"

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

  1. 我已将.p12证书上传到Firebase上进行开发和生产
  2. 我已经检查了我的应用和Firebase控制台上的捆绑包ID
  3. 我在我的应用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");

}

请帮助.

推荐答案

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

I got solution to my own problem. The device's date and time was incorrect. The second I changed it to current date & time, Firebase started giving me FCM token and got connected properly. I have checked the push notification as well from Firebase Notification Console. It is working in a way better than I imagined.

这篇关于无法在真实设备上获取FCM令牌,但正在模拟器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆