【问题标题】:Firebase InstanceID.instanceID().token() method is deprecatedFirebase InstanceID.instanceID().token() 方法已弃用
【发布时间】:2018-11-29 09:33:04
【问题描述】:

我正在使用 swift 和 firebase。以前我使用以下方法获取 firebase 令牌,然后我将其存储到数据库中以发送通知。

InstanceID.instanceID().token()

现在这个方法显示为已弃用,因为我已经更新了我的 firebase。

'token()' is deprecated: Use instanceIDWithHandler: instead.

我不知道如何使用instanceIDWithHandler 我尝试过关注但不知道如何获取令牌。

func instanceID(handler: @escaping InstanceIDResultHandler){

    }

请帮忙。 提前谢谢你。

【问题讨论】:

  • 你问的是this吗?

标签: swift firebase firebase-cloud-messaging token


【解决方案1】:

Fetching the current registration token

注册令牌通过messaging:didReceiveRegistrationToken: 方法传递。此方法通常在每个应用程序以 FCM 令牌启动时调用一次。调用此方法时,最理想的时机是:

  • 如果注册令牌是新的,请将其发送到您的应用服务器。
  • 为主题订阅注册令牌。这仅适用于新订阅或用户重新安装应用的情况。

您可以使用 instanceIDWithHandler: 直接检索令牌。此回调提供一个 InstanceIDResult,其中包含令牌。如果 InstanceID 检索以任何方式失败,则会提供非 null 错误。

您应该导入 FirebaseInstanceID

  import FirebaseInstanceID

目标 C

在您的 getTokenMethod 上

[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
                                                NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Error fetching remote instance ID: %@", error);
    } else {
        NSLog(@"Remote instance ID token: %@", result.token);
    }
}];

斯威夫特

InstanceID.instanceID().instanceID { result, error in
    if let error = error {
        print("Error fetching remote instange ID: \(error)")
    } else if let result = result {
        print("Remote instance ID token: \(result.token)")
    }
}

【讨论】:

  • 此解决方案运行完美,谢谢先生。我想知道你从哪里得到这个答案或者我在哪里可以学到这些东西。当我阅读 firebase 文档时,我发现 link 根本没有用,或者我无法理解它。
  • @user9496018 包含此信息的文档在这里:firebase.google.com/docs/cloud-messaging/ios/first-message
  • 我们的一些用户对这个新解决方案有疑问,lambda 永远不会返回结果。
  • 同@PedroPauloAmorim :(
  • 同样的问题,lambda 永远不会返回结果!
【解决方案2】:

InstanceID 现已弃用。试试

Messaging.messaging().token { token, error in
   // Check for error. Otherwise do what you will with token here
}

Documentation on Fetching the current registration token

【讨论】:

    【解决方案3】:

    解决办法

    问题是 FirebaseInstanceID 最近已被弃用

    InstanceID.instanceID().instanceID {result, _ in

    新的

    Messaging.messaging().token { (token, _) in

    你可以像上面那样替换它,它可以工作

    查看此链接

    https://medium.com/nerd-for-tech/how-to-solve-capacitor-error-no-such-module-firebaseinstanceid-9c142933b589

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-08
      • 2012-09-20
      • 2020-11-28
      • 1970-01-01
      • 2020-11-19
      • 2012-03-04
      • 2019-07-26
      相关资源
      最近更新 更多