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

时间:2017-01-21 10:37:07

标签: ios firebase-cloud-messaging

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

  

无法获取默认令牌Error Domain = com.firebase.iid Code = 501   “(空)”

  1. 我上传了用于开发的.p12证书,并在Firebase上发布了
  2. 我已检查过我的应用的捆绑包ID以及Firebase控制台上的捆绑包
  3. 我的应用ID已启用推送通知。
  4. 这是我的代码

    - (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");
    
    }
    

    请帮忙。

2 个答案:

答案 0 :(得分:2)

我解决了自己的问题。设备的日期和时间不正确。第二个我改为现在的日期&时间,Firebase开始给我FCM令牌并正确连接。我也从Firebase Notification Console检查了推送通知。它的工作方式比我想象的要好。

答案 1 :(得分:0)

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