0

May I ask a question that I can use "if" for FirebaseInstanceId? I have tried to make a sign up an account page and nothing happens or shows and I have an error message:

import android.support.annotation.NonNull;

mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>(){
@Override
public void onComplete(@NonNull Task<AuthResult> task){

        if(task.isSuccessful()){
           // these lines for taking DEVICE TOKEN for sending device to device notification
           String userUID=mAuth.getCurrentUser().getUid();
           String userDeiceToken=FirebaseInstanceId.getInstance().getToken();
           userDatabaseReference.child(userUID).child("device_token").setValue(userDeiceToken)
        .             addOnSuccessListener(new OnSuccessListener<Void>(){
                @Override
                public void onSuccess(Void aVoid){
                    checkVerifiedEmail();
                }
            });
        }
Sunny Yeung
  • 1
  • 1
  • 2

2 Answers2

3

The FirebaseInstanceId getToken() method is deprecated.

From the docs:

This method is deprecated. In favour of getInstanceId().

There are a couple of things you need to be aware of due to this change:

  1. You do not need to use FirebaseInstanceIdService to receive a token. Instead use FirebaseMessagingService
  2. Override onNewToken() in FirebaseMessagingService
  3. Update how the token is accessed

The code below should address your issues:

public class MyFirebaseMessagingService extends FirebaseMessagingService {    
    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        Log.d("TOKEN", token);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
    }
}

If you need to retrieve the token you can use the method below:

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this, 
new OnSuccessListener<InstanceIdResult>() {
    @Override
    public void onSuccess(InstanceIdResult instanceIdResult) {
        String newToken = instanceIdResult.getToken();
        Log.d("newToken", newToken);
    }
});

The code here I reference from this article.

haldo
  • 14,512
  • 5
  • 46
  • 52
1

According to the documentation, the method getToken() is deprecated, which means that it will be removed in the next releases. Documentation says that you should use another method to receive token - getInstanceId();

For more info please follow the link: https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId.html#getToken()

Naya
  • 850
  • 6
  • 19