【问题标题】:Firebase Refresh TokenFirebase 刷新令牌
【发布时间】:2016-11-21 12:21:34
【问题描述】:

使用方法

[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]

我不太确定参数要求什么?授权实体和行动是什么?我是否也将 Apple 的 APNS 令牌传递给该方法?

【问题讨论】:

  • 避免调用.getToken(authorizedEntity, scope),除非需要启用多个发件人。请改用instanceIDWithHandler:

标签: ios objective-c firebase firebase-cloud-messaging


【解决方案1】:

你可以这样做。

[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];

[[FIRInstanceID instanceID] tokenWithAuthorizedEntity:gcmSenderID scope:kFIRInstanceIDTokenRefreshNotification options:nil handler:^(NSString * _Nullable token, NSError * _Nullable error) {

    NSLog(@"GCM Registration token = %@",token);
    NSLog(@"GCM Registration error = %@",error);        
}];

【讨论】:

    【解决方案2】:
    1. AUTHORIZED_ENTITY - 基本上它要求提供 google 项目 ID。它是数字的,如果您之前已经在项目中集成了 GCM,它将是 GCM_SENDER_ID(类似于“568520103762”)。检查您的 Google-info.plist 以找到它。
    2. 范围 - kFIRInstanceIDScopeFirebaseMessaging
    3. OPTIONS - @{@"apns_token": deviceToken}(您将在 didRegisterForRemoteNotifications 方法中获得 DeviceToken)
    4. HANDLER - 如果您已收到令牌,则捕获令牌或在此处捕获错误。如果token为nil,则在“tokenRefreshNotification”方法中等待token,如果[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]中token为nil则自动调用该方法

    例子:

     if (![[FIRInstanceID instanceID] token]) {
        [[FIRInstanceID instanceID] tokenWithAuthorizedEntity:_gcmSenderId scope:kFIRInstanceIDScopeFirebaseMessaging options:_registrationOptions handler:^(NSString * _Nullable token, NSError * _Nullable error) {
    
            // Fetch the token or error
        }];
    
    }
    

    【讨论】:

    • 这应该是被接受的答案,详细描述了每个领域。好的! :)
    • 嘿,这听起来很合理。但是,你知道为什么我在这个处理程序块中出现错误:Error Domain=com.firebase.iid Code=1002 "(null)"
    【解决方案3】:

    Swift 的版本(基于@HeadOnn 的answer):

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
    {
        Messaging.messaging().setAPNSToken(deviceToken, type: .prod) // may be excess
    
        guard let plistPath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist"),
            let options = FirebaseOptions(contentsOfFile: plistPath)
        else { return }
    
        InstanceID.instanceID().token(withAuthorizedEntity: options.gcmSenderID, 
                scope: InstanceIDScopeFirebaseMessaging,
                options: ["apns_token": deviceToken])
        { (token, error) in
            // handle token and error
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-16
      • 2017-07-25
      • 2017-06-30
      • 2016-10-08
      • 2017-10-19
      • 2018-01-31
      • 2021-04-30
      • 2021-08-09
      • 2018-03-16
      相关资源
      最近更新 更多