我正在编写一个聊天应用程序,我想将 token ID 保存在 firebase 数据库中,但 token 为空。这是我的代码:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String user_id = mAuth.getCurrentUser().getUid();
String Device_id = FirebaseInstanceId.getInstance().getToken();
Toast.makeText(this, Device_id, Toast.LENGTH_SHORT).show();
mDatabaseTokenId.child(user_id).child("device_token").setValue(Device_id).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
}else{
Toast.makeText(LoginActivity.this, ""+task.getException()
, Toast.LENGTH_SHORT).show();
}
}
});
if(user.isEmailVerified()){
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
device_id为什么返回空?请帮助我,伙计们。
最佳答案
在 list 中添加服务
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
您的服务中将有一个 onTokenRefresh 方法,如下所示
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
关于java - android studio firebase gettoken id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45781942/