如何处理FirebaseInstanceId.getInstance()。getToken()= null [英] How to handle FirebaseInstanceId.getInstance().getToken() = null
问题描述
我的问题是特定于用户第一次安装我的应用程序,由于某些原因,无法从onTokenRefresh收到注册ID。我们该如何处理呢?我可以从我的FirebaseInstanceIdService类中设置一个广播接收器,它会在收到注册标识时通知主要活动?
- 如果您的设备没有连接到互联网 onTokenRefresh()永远不会被呼叫,您应该通知用户他/她的设备没有互联网连接
- firebase拥有自己的网络更改监听器,当连接到互联网的设备尝试获取令牌并返回时,此时可以通过向本地广播接收器发送注册令牌来告诉您的主要活动。 >
使用以下代码:
@Override
public void onTokenRefresh(){
//获取更新的InstanceID标记。
String refreshedToken = FirebaseInstanceId.getInstance()。getToken();
Log.d(FCN TOKEN GET,刷新标记:+刷新标记);
final Intent intent = new Intent(tokenReceiver);
//你也可以包含一些额外的数据。
final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
intent.putExtra(token,refreshedToken);
broadcastManager.sendBroadcast(intent);
$ b}
在您的主要活动中:
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));
$ b BroadcastReceiver tokenReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context,Intent intent){
String token = intent.getStringExtra(token);
if(token!= null)
{
//发送令牌给你的服务器或你想做什么
}
}
};
}
I have just migrated to FCM. I have added my class that extends from FirebaseInstanceIdService to receive a refreshedToken as and when appropriate.
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?
- 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 it's 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.
use below codes:
@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);
}
in your main activity:
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屋!