gpt4 book ai didi

swift - Firebase InstanceID.instanceID().token() 方法已弃用

转载 作者:IT王子 更新时间:2023-10-29 05:28:43 26 4
gpt4 key购买 nike

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

InstanceID.instanceID().token()

自从我更新了我的 firebase 后,此方法显示为已弃用。

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

我不知道如何使用 instanceIDWithHandler 我尝试了以下但不知道如何获取 token 。

func instanceID(handler: @escaping InstanceIDResultHandler){

}

请帮忙。提前谢谢你。

最佳答案

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.
  • 将注册 token 订阅到主题。这仅适用于新订阅或用户重新安装应用的情况。

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

  import FirebaseInstanceID

objective-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);
}
}];

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)")
}
}

更新

InstanceID 现已弃用。尝试

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

关于swift - Firebase InstanceID.instanceID().token() 方法已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50945015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com