gpt4 book ai didi

ios - 无法在真实设备上获得 FCM token 但在模拟器上获得

转载 作者:行者123 更新时间:2023-12-03 13:16:54 25 4
gpt4 key购买 nike

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

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


  • 我已经上传了 .p12 证书用于 Firebase 上的开发和生产
  • 我已经检查了我的应用程序和 Firebase 控制台上的包 ID
  • 我的 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");

    }

    请帮忙。

    最佳答案

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

    关于ios - 无法在真实设备上获得 FCM token 但在模拟器上获得,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41778308/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com