如何处理 FirebaseInstanceId.getInstance().getToken() = null [英] How to handle FirebaseInstanceId.getInstance().getToken() = null

查看:20
本文介绍了如何处理 FirebaseInstanceId.getInstance().getToken() = null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚迁移到 FCM.我添加了从 FirebaseInstanceIdService 扩展的类,以便在适当的时候接收 refreshedToken.

I have just migrated to FCM. I have added my class that extends from FirebaseInstanceIdService to receive a refreshedToken as and when appropriate.

我的问题是针对用户第一次安装我的应用程序并且由于某种原因无法从 onTokenRefresh 接收注册 ID 的情况.我们应该如何处理这件事?我可以从我的 FirebaseInstanceIdService 类中设置一个广播接收器,它会在收到注册 ID 时通知 Main 活动吗?

My question is specific to the case when user installs my app first time and due to some reason, unable to receive a registration Id from onTokenRefresh. How are we supposed to handle this? Can I set a broadcast receiver from my FirebaseInstanceIdService class which will notify the Main activity when a registration Id is received?

推荐答案

  • 如果您的设备没有连接到互联网 onTokenRefresh() 永远不会被调用,您应该通知用户他/她的设备没有互联网连接
  • firebase 有自己的网络更改侦听器,当设备连接到互联网时,然后尝试获取令牌并返回它,此时您可以通过发送本地广播接收器来告知您的主要活动,即接收到注册令牌.

    • if your device have no connection to the internet onTokenRefresh() is never called and you should notify to user his/her device has no internet connection
    • firebase has its own network change listener and when a device connected to the internet then try to get token and return it, at this time you can tell your main activity by sending a local broadcast receiver that registration token is received.
    • 使用以下代码:

          @Override
      public void onTokenRefresh() {
      
          // Get updated InstanceID token.
          String refreshedToken = FirebaseInstanceId.getInstance().getToken();
      
          Log.d("FCN TOKEN GET", "Refreshed token: " + refreshedToken);
      
          final Intent intent = new Intent("tokenReceiver");
          // You can also include some extra data.
          final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
          intent.putExtra("token",refreshedToken);
          broadcastManager.sendBroadcast(intent);
      
      
      }
      

      在您的主要活动中:

          public class MainActivity extends AppCompatActivity {
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_splash);
         LocalBroadcastManager.getInstance(this).registerReceiver(tokenReceiver,
                  new IntentFilter("tokenReceiver"));
      
      }
      
      BroadcastReceiver tokenReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              String token = intent.getStringExtra("token");
              if(token != null)
              {
                  //send token to your server or what you want to do
              }
      
          }
      };
      
      }
      

      这篇关于如何处理 FirebaseInstanceId.getInstance().getToken() = null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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