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

Firebase InstanceID.instanceID().token() method is deprecated(Firebase InstanceID.instanceID().token() 方法已弃用)
本文介绍了Firebase InstanceID.instanceID().token() 方法已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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.

  • Subscribe the registration token to topics. This is required only for new subscriptions or for situations where the user has re-installed the app.

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.

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() 方法已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
appearance().setBackgroundImage Not Working On Custom Class(外观().setBackoundImage在自定义类上不起作用)
Show/Hide barButtonItem(显示/隐藏barButtonItem)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)