我使用的是最新版本的android(Androidx(。.setValue(deviceToken)
不识别OnSuccessListener
函数中的deviceToken
。我已经在build.gradle
中安装了implementation 'com.google.firebase:firebase-messaging:20.0.1'
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( new OnSuccessListener<InstanceIdResult>()
{
@Override
public void onSuccess(InstanceIdResult instanceIdResult)
{
String deviceToken = instanceIdResult.getToken();
Log.e("newToken",deviceToken);
}
});
RootRef.child("Users").child(currentUserId).child("device_token")
.setValue(deviceToken);
您可以使用addOnCompleteListener:它运行良好。
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if(!task.isSuccessful()){
Log.w("newToken", "getInstanceId failed", task.getException());
}else{
String deviceToken = task.getResult().getToken();
Log.e("newToken",deviceToken);
RootRef.child("Users").child(currentUserId).child("device_token")
.setValue(deviceToken);
}
}
});