ClassMyFirebaseIdService.java
public class MyFirebaseIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
FirebaseUser firebaseUser= FirebaseAuth.getInstance().getCurrentUser();
String refreshToken= FirebaseInstanceId.getInstance().getToken();
if (firebaseUser !=null)
{
updateToken(refreshToken);
}
}
private void updateToken(String refreshToken)
{
FirebaseUser firebaseUser=FirebaseAuth.getInstance().getCurrentUser();
Token token=new Token(refreshToken);
FirebaseDatabase.getInstance().getReference("Tokens")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(token);
}
}
Chatfragment.java类
private void UpdateToken() {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String refreshToken = FirebaseInstanceId.getInstance().getToken();
Token token = new Token(refreshToken);
FirebaseDatabase.getInstance().getReference("Tokens").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(token);
}
FirebaseInstanceIdService
早就被弃用了。因此,它将不再起作用。使用FirebaseMessagingService
并覆盖onNewToken
方法,该方法也将为您提供新的令牌。所以按照使用
public class MyFirebaseIdService extends FirebaseMessagingService {
@Override
public void onNewToken(String token) {
updateToken(token);
}
private void updateToken(String refreshToken)
{
FirebaseUser firebaseUser=FirebaseAuth.getInstance().getCurrentUser();
Token token=new Token(refreshToken);
FirebaseDatabase.getInstance().getReference("Tokens")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(token);
}
}
有关Firebase消息的最新版本,请访问最新SDK版本
希望它对你有用。