我正在使用 swift 和 firebase.以前我使用以下方法获取 firebase 令牌,然后我将其存储到数据库中以发送通知.
InstanceID.instanceID().token()
现在这种方法显示为已弃用,因为我已经更新了我的 firebase.
'token()' 已弃用:使用 instanceIDWithHandler: 代替.
我不知道如何使用 instanceIDWithHandler
我尝试了以下但不知道如何获取令牌.
func instanceID(handler: @escaping InstanceIDResultHandler){}
请帮忙.提前谢谢你.
获取当前注册令牌
<块引用>注册令牌通过 messaging:didReceiveRegistrationToken:
方法传递.此方法通常在每个应用程序以 FCM 令牌启动时调用一次.调用此方法时,最理想的时机是:
<块引用>
您可以使用 instanceIDWithHandler:
直接检索令牌.此回调提供一个 InstanceIDResult
,其中包含令牌.如果 InstanceID 检索以任何方式失败,则会提供非 null 错误.
您应该导入 FirebaseInstanceID
导入 FirebaseInstanceID
目标 C
在您的 getTokenMethod 上
[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable 结果,NSError * _Nullable 错误){如果(错误!= nil){NSLog(@"Error fetching remote instance ID: %@", error);} 别的 {NSLog(@"Remote instance ID token: %@", result.token);}}];
斯威夫特
InstanceID.instanceID().instanceID { 结果,错误如果让错误=错误{print("获取远程实例 ID 时出错:(error)")} else if let result = result {print("远程实例 ID 令牌:(result.token)")}}
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()
Now this method is showing as deprecated since i have updated my firebase.
'token()' is deprecated: Use instanceIDWithHandler: instead.
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.
Fetching the current registration token
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:
- If the registration token is new, send it to your application server.
You can retrieve the token directly using
instanceIDWithHandler:
. This callback provides anInstanceIDResult
, which contains the token. A non null error is provided if the InstanceID retrieval failed in any way.
You should import FirebaseInstanceID
import FirebaseInstanceID
objective C
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);
}
}];
Swift
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() 方法已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!