【问题标题】:Unable to get FCM token on real device but getting on simulator无法在真实设备上获取 FCM 令牌但进入模拟器
【发布时间】:2017-06-06 07:22:59
【问题描述】:

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

未能获取默认令牌错误域=com.firebase.iid 代码=501 “(空)”

  1. 我已上传 .p12 证书,用于 Firebase 上的开发和产品
  2. 我已经检查了我的应用和 Firebase 控制台上的捆绑 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 你能帮帮我吗?
  • 在功能中启用一次钥匙串共享并检查其是否有效,同时确保您已在功能中启用推送通知。
  • 我已经这样做了。知道为什么我在模拟器上获得令牌而不是在设备上。

标签: ios firebase-cloud-messaging


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 2016-05-29
      • 2016-05-15
      • 2017-10-28
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      相关资源
      最近更新 更多