不推荐使用Firebase InstanceID.instanceID().token()方法

Dee*_*pak 19 token firebase swift firebase-cloud-messaging

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

InstanceID.instanceID().token()
Run Code Online (Sandbox Code Playgroud)

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

'token()' is deprecated: Use instanceIDWithHandler: instead.
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用instanceIDWithHandler我试过以下但不知道如何获得令牌.

func instanceID(handler: @escaping InstanceIDResultHandler){

    }
Run Code Online (Sandbox Code Playgroud)

请帮忙.先感谢您.

小智 46

InstanceID现在已弃用。尝试

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

请参阅有关获取当前注册令牌的文档


Anb*_*hik 45

获取当前注册令牌

注册令牌通过该方法提供messaging:didReceiveRegistrationToken:.通常每个应用程序启动时使用FCM令牌调用此方法一次.调用此方法时,理想的时间是:

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

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

目标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);
}
}];
Run Code Online (Sandbox Code Playgroud)

迅速

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)")
 }
}
Run Code Online (Sandbox Code Playgroud)

  • 我们的某些用户对此新解决方案有疑问,lambda永远不会返回结果。 (4认同)
  • 和@PedroPauloAmorim一样:( (2认同)

Pra*_*amy 9

这里是解决方案

问题是 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