gpt4 book ai didi

ios - iOS设备上的Firebase InstanceID token 刷新延迟

转载 作者:行者123 更新时间:2023-12-01 20:00:28 24 4
gpt4 key购买 nike

我在iOS应用程序中实现了Firebase Cloud Messaging。除了一件事,一切似乎都可以正常工作,事实是从应用程序启动到将要在Firebase中刷新设备 token 大约需要10秒钟。

我已将此代码添加到App Delegate中的application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中:

[FIRApp configure];

//Add an observer for handling a token refresh callback.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshCallback:) name:kFIRInstanceIDTokenRefreshNotification object:nil];

这是 tokenRefreshCallback:方法:
- (void)tokenRefreshCallback:(NSNotification *)notification {

NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"InstanceID token: %@", refreshedToken);

if ([[FIRInstanceID instanceID] token] != NULL) {
[[FIRMessaging messaging] subscribeToTopic:@"/topics/news"];
NSLog(@"Subscribed to news topic");
}

//Connect to FCM since connection may have failed when attempting before having a token
[self connectToFirebase];

}

这是记录器的相关部分:
2016-10-13 14:36:23.844 My-App[1111] <Debug> [Firebase/Core][I-COR000001] Configuring the default app.
2016-10-13 14:36:26.492 My-App[1111:2222222] InstanceID token: (null)
2016-10-13 14:36:32.732 My-App[1111:2222222] InstanceID token: c1kmaskdmj...(the actual device token)
  • 当InstanceID为(null)时为什么调用tokenRefreshCallback:
  • 为什么要花大约10秒钟才能检索到实际 token ?
  • 最佳答案

    就我而言。使用深层链接在屏幕之间导航时,我遇到了同样的问题。

    没有这些,一切运行良好。

    这为我工作。在应用程序didFinishLaunchingWithOptions中,我调用[self configureFirebase];

    - (void)configureFirebase {
    [FIRApp configure];
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else {
    // iOS 10 or later
    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    UNAuthorizationOptions authOptions =
    UNAuthorizationOptionAlert
    | UNAuthorizationOptionSound
    | UNAuthorizationOptionBadge;
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
    }];

    // For iOS 10 display notification (sent via APNS)
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    // For iOS 10 data message (sent via FCM)
    [FIRMessaging messaging].remoteMessageDelegate = self;
    #endif
    }

    [[UIApplication sharedApplication] registerForRemoteNotifications];
    }

    我也实现以下消息:
    // [START receive_message]
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    if (!(application.applicationState == UIApplicationStateActive)) {
    [AuthenticationController showAuthenticationViews:self.window];
    }
    [self readNotification:userInfo];
    }

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [self readNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    }
    // [END receive_message]

    // [START ios_10_message_handling]
    // Receive displayed notifications for iOS 10 devices.
    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    // Handle incoming notification messages while app is in the foreground.
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;
    [self readNotification:userInfo];
    completionHandler(UNNotificationPresentationOptionNone);
    }

    // Handle notification messages after display notification is tapped by the user.
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    [self readNotification:userInfo];
    completionHandler();
    }
    #endif
    // [END ios_10_message_handling]

    // [START ios_10_data_message_handling]
    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    // Receive data message on iOS 10 devices while app is in the foreground.
    - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
    // Print full message
    //NSLog(@"%@", remoteMessage.appData);
    }
    #endif
    // [END ios_10_data_message_handling]

    // [START refresh_token]
    - (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.
    }
    // [END refresh_token]

    // [START connect_to_fcm]
    - (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 {
    [[FIRMessaging messaging] subscribeToTopic:notificationTopic];
    //NSLog(@"Connected to FCM.");
    }
    }];
    }
    // [END connect_to_fcm]

    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    // NSLog(@"Unable to register for remote notifications: %@", error);
    }

    // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
    // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
    // the InstanceID token.
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // NSLog(@"APNs token retrieved: %@", deviceToken);

    // With swizzling disabled you must set the APNs token here.
    // [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
    BOOL automatic = [[[NSUserDefaults standardUserDefaults] objectForKey:@"automaticLogin"] boolValue];
    if (!automatic) {
    [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"automaticLogin"];
    [self moveToInit];
    }
    }

    // [START connect_on_active]
    - (void)applicationDidBecomeActive:(UIApplication *)application {
    [self connectToFcm];
    }
    // [END connect_on_active]

    // [START disconnect_from_fcm]
    - (void)applicationDidEnterBackground:(UIApplication *)application {
    [[FIRMessaging messaging] disconnect];
    // NSLog(@"Disconnected from FCM");
    }
    // [END disconnect_from_fcm]

    #pragma mark - Process notifications



    - (void)readNotification:(NSDictionary *)userInfo {
    //read your notification
    }

    -(void)readLaunchWithOptions:(NSDictionary*)launchOptions{
    if (launchOptions[notificationKey]) {
    [self readNotification:launchOptions[notificationKey]];
    }
    }

    关于ios - iOS设备上的Firebase InstanceID token 刷新延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40021541/

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