不推荐使用Firebase InstanceID.instanceID().token()方法 [英] Firebase InstanceID.instanceID().token() method is deprecated
问题描述
我正在使用swift和firebase.以前,我使用以下方法获取firebase令牌,然后将其用于存储到数据库中以发送通知.
I am working with swift and firebase. Previously I was using following method to get firebase token which then I was using to store into database to send notifications.
InstanceID.instanceID().token()
现在,由于我已经更新了Firebase,因此该方法已被弃用.
Now this method is showing as deprecated since i have updated my firebase.
'token()' is deprecated: Use instanceIDWithHandler: instead.
我不知道如何使用我尝试过的instanceIDWithHandler
,但不知道如何获得令牌.
I don't know how to use instanceIDWithHandler
i have tried following but don't know how to get token.
func instanceID(handler: @escaping InstanceIDResultHandler){
}
请帮助. 预先谢谢你.
Please help. Thank you in advance.
推荐答案
注册令牌是通过方法
messaging:didReceiveRegistrationToken:
传递的.通常每个应用程序以FCM令牌开始一次调用此方法.调用此方法时,是进行以下操作的理想时间:
Registration tokens are delivered via the method
messaging:didReceiveRegistrationToken:
. This method is called generally once per app start with an FCM token. When this method is called, it is the ideal time to:
- 如果注册令牌是新的,请将其发送到您的应用程序服务器.
- 为主题注册注册令牌.仅对于新订阅或用户重新安装应用程序的情况,才需要这样做.
您可以直接使用 instanceIDWithHandler:
检索令牌.该回调提供了一个 InstanceIDResult
,其中包含令牌.如果InstanceID检索以任何方式失败,将提供一个非null的错误.
You can retrieve the token directly using instanceIDWithHandler:
. This callback provides an InstanceIDResult
, which contains the token. A non null error is provided if the InstanceID retrieval failed in any way.
您应该导入FirebaseInstanceID
You should import FirebaseInstanceID
import FirebaseInstanceID
目标C
在您的getTokenMethod上
on your 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 InstanceID.instanceID().token()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!