<small id='ffJrP'></small><noframes id='ffJrP'>

  • <legend id='ffJrP'><style id='ffJrP'><dir id='ffJrP'><q id='ffJrP'></q></dir></style></legend>

    <tfoot id='ffJrP'></tfoot>

      <bdo id='ffJrP'></bdo><ul id='ffJrP'></ul>

        <i id='ffJrP'><tr id='ffJrP'><dt id='ffJrP'><q id='ffJrP'><span id='ffJrP'><b id='ffJrP'><form id='ffJrP'><ins id='ffJrP'></ins><ul id='ffJrP'></ul><sub id='ffJrP'></sub></form><legend id='ffJrP'></legend><bdo id='ffJrP'><pre id='ffJrP'><center id='ffJrP'></center></pre></bdo></b><th id='ffJrP'></th></span></q></dt></tr></i><div id='ffJrP'><tfoot id='ffJrP'></tfoot><dl id='ffJrP'><fieldset id='ffJrP'></fieldset></dl></div>
      1. 我现在应该使用什么方法,因为 FirebaseInstanceId.getInstance().getToken() 已

        时间:2023-07-30

            <legend id='cYXNb'><style id='cYXNb'><dir id='cYXNb'><q id='cYXNb'></q></dir></style></legend>
              <tbody id='cYXNb'></tbody>

            <i id='cYXNb'><tr id='cYXNb'><dt id='cYXNb'><q id='cYXNb'><span id='cYXNb'><b id='cYXNb'><form id='cYXNb'><ins id='cYXNb'></ins><ul id='cYXNb'></ul><sub id='cYXNb'></sub></form><legend id='cYXNb'></legend><bdo id='cYXNb'><pre id='cYXNb'><center id='cYXNb'></center></pre></bdo></b><th id='cYXNb'></th></span></q></dt></tr></i><div id='cYXNb'><tfoot id='cYXNb'></tfoot><dl id='cYXNb'><fieldset id='cYXNb'></fieldset></dl></div>
          1. <small id='cYXNb'></small><noframes id='cYXNb'>

                <bdo id='cYXNb'></bdo><ul id='cYXNb'></ul>

                1. <tfoot id='cYXNb'></tfoot>

                  本文介绍了我现在应该使用什么方法,因为 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.

                  推荐答案

                  更新答案

                  FirebaseInstanceId 已弃用,但现在您可以使用 FirebaseMessaging.getInstance().token.

                  FirebaseInstanceId is deprecated but now you can use FirebaseMessaging.getInstance().token.

                  例如:

                  FirebaseMessaging.getInstance().token.addOnSuccessListener { result ->
                          if(result != null){
                              fbToken = result
                              // DO your thing with your firebase token
                          }
                  }
                  

                  老答案

                  作为 文档 说:

                  此方法已被弃用.赞成 getInstanceId().

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

                  getInstanceId() 将返回一个带有 InstanceIdResult 的任务.像这样:

                  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:(是的,Messaging",而不是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() 已被弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Firebase 云消息传递的主题? 下一篇:firebase的app token在什么时期发生变化,如何管理?

                  相关文章

                  <legend id='HrD3z'><style id='HrD3z'><dir id='HrD3z'><q id='HrD3z'></q></dir></style></legend>

                    <small id='HrD3z'></small><noframes id='HrD3z'>

                      <bdo id='HrD3z'></bdo><ul id='HrD3z'></ul>

                  1. <i id='HrD3z'><tr id='HrD3z'><dt id='HrD3z'><q id='HrD3z'><span id='HrD3z'><b id='HrD3z'><form id='HrD3z'><ins id='HrD3z'></ins><ul id='HrD3z'></ul><sub id='HrD3z'></sub></form><legend id='HrD3z'></legend><bdo id='HrD3z'><pre id='HrD3z'><center id='HrD3z'></center></pre></bdo></b><th id='HrD3z'></th></span></q></dt></tr></i><div id='HrD3z'><tfoot id='HrD3z'></tfoot><dl id='HrD3z'><fieldset id='HrD3z'></fieldset></dl></div>

                      <tfoot id='HrD3z'></tfoot>