FirebaseInstanceId.getInstance().getToken()已被弃用,现在该使用什么 [英] What to use now that FirebaseInstanceId.getInstance().getToken() is deprecated

查看:1029
本文介绍了FirebaseInstanceId.getInstance().getToken()已被弃用,现在该使用什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于不赞成使用getToken(),我想知道什么是获取Firebase令牌以发送推送通知的正确方法.

I would like to know what would be the correct way to get Firebase token for sending push notification now that getToken() is deprecated.

推荐答案

不推荐使用此方法. 支持getInstanceId().

This method was deprecated. In favour of getInstanceId().

getInstanceId()将返回带有和InstanceIdResult的Task.像这样:

getInstanceId() will return a Task with and InstanceIdResult. Like this:

 FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( new OnSuccessListener<InstanceIdResult>() {                    
                @Override
                public void onSuccess(InstanceIdResult instanceIdResult) {
                      String deviceToken = instanceIdResult.getToken();
                      // Do whatever you want with your token now
                      // i.e. store it on SharedPreferences or DB
                      // or directly send it to server 
                }
});

更新:

尽管确实可以使用此方法代替 FirebaseInstanceId.getInstanceId().getToken(),但它不能解决不赞成使用 FirebaseInstanceIdService 的事实给我们另一个问题是:在哪里使用它? 它可以在任何Activity上下文中使用,它将始终返回令牌.但是,如果我们只想在创建时以及很少更新令牌时获取令牌,该怎么办? 为此,您应该覆盖旧的 FirebaseMessagingService 实现中的新方法 onNewToken : (是的,消息传递",而不是"InstanceId")

Though is true that this approach will literally replace the use of FirebaseInstanceId.getInstanceId().getToken(), it does not solve the fact that FirebaseInstanceIdService is also deprecated leaving us with another question that is: where to use it? It can be used in any Activity context that it will always return the token. But what if we want to get the token only on creation and when it is rarely updated? For that you should override new method onNewToken from our old FirebaseMessagingService implementation: (Yes, "Messaging", not "InstanceId")

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    String deviceToken = s;
    // Do whatever you want with your token now
    // i.e. store it on SharedPreferences or DB
    // or directly send it to server 
}

这样,代码将保持精简,甚至不需要使用第一种方法.

This way code will remain leaner and wont even be necessary to use the first approach.

这篇关于FirebaseInstanceId.getInstance().getToken()已被弃用,现在该使用什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆