【问题标题】:iOS & FCM: Force Refresh TokensiOS 和 FCM:强制刷新令牌
【发布时间】:2016-06-15 01:33:15
【问题描述】:

有谁知道如何强制刷新 FCM 生成的注册令牌?我最近才尝试将我的项目从 GCM 迁移到 FCM。

Firebase 网站上的文档提到使用以下方法生成令牌:

// Get the default token
// The first time you call this, the token may not be available, in which case
// the SDK returns nil.
// Once the token is fetched from the server, the SDK posts a token refresh
// notification that you can listen for in order to access the new token.

NSString *token = [[FIRInstanceID instanceID] token];

但看起来我只得到了我调用FIRInstanceID.instanceID().token()时最初生成的令牌

【问题讨论】:

    标签: ios swift google-cloud-messaging firebase-cloud-messaging


    【解决方案1】:

    如果 Instance ID 失效,应用可以请求一个新的,并将其发送到应用服务器。要证明实例 ID 的所有权并允许服务器访问与应用关联的数据或服务,请调用 [FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]。

    https://firebase.google.com/docs/reference/ios/firebaseinstanceid/interface_f_i_r_instance_i_d

    【讨论】:

      【解决方案2】:

      可能来晚了,但如果有任何帮助,其他任何人都可以参考我的答案。我同意@mKane,但我想在他的回答中添加更多内容。 如果你重置了instanceID或者删除了token,可以调用

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

      如果token为nil,则在“tokenRefreshNotification”方法中等待token,如果在[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]中token为nil,该方法会自动调用。然后,您可以在“tokenRefreshNotification”方法中捕获令牌并使用它。

      这个方法的使用方法请参考我的回答here

      【讨论】:

        【解决方案3】:

        在 swift 5 中强制刷新我所做的令牌:

        InstanceID.deleteID(InstanceID.instanceID())(handler: {_ in}) 
        

        正如文档所说: 此方法还会触发获取新实例 ID 和 Firebase 消息传递范围令牌的请求。新 ID 和令牌准备好后,请收听 kFIRInstanceIDTokenRefreshNotification。

        然后要获取新的令牌,照常进行:

        InstanceID.instanceID().instanceID { (result, error) in
        if error == nil{
            let newToken = result.token
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-29
          • 2022-10-16
          • 2020-03-18
          • 2020-09-19
          • 2013-03-28
          • 2017-03-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多