Ezequiel Adrian
Ezequiel Adrian

Reputation: 806

Firebase Token is inconsistent between API calls

I am using Firebase FCM to send notifications from my server to the users.

When the user installs the App for the first time, i catch the fresh and new token at MessagingService.java:

@Override
public void onNewToken(@NonNull String tkn) {
    super.onNewToken(tkn);
    sendTokenToServer(tkn);
}

Here comes the problem, when the user closes session (without uninstalling the app), SharedPreferences are deleted. A new session is started; but onNewToken() is not called. So, i must manually retrieve the Token inside my MainActivity in order to send it to the server. I am getting the updated token with this piece of code:

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MainActivity.this, new OnSuccessListener<InstanceIdResult>() {
    @Override
    public void onSuccess(InstanceIdResult instanceIdResult) {
        sendTokenToServer(instanceIdResult.getToken());
    }
});

As you know, that code is deprecated and should be avoided. Instead, i tried to replace it with this piece of code with no success:

FirebaseInstallations.getInstance().getToken(true).addOnCompleteListener(new OnCompleteListener<InstallationTokenResult>() {
    @Override
    public void onComplete(@NonNull Task<InstallationTokenResult> task) {
        if(task.isSuccessful()) {
            String token = task.getResult().getToken();
        }
    }
});

The Token length obtained at onNewToken() is 163. The token length obtained at deprecated call is 163 (perfect, but deprtecated). The token length obtained at FirebaseInstallations is 316.

My firebase API at server side fails to send a notification using the code of 316 length. Any one knows what i am doing wrong? Or why i get those different length tokens?

Update:

Server side python, retrieves token from database and sends the notification like this. Please note this code is working when token len is 163.

from pyfcm import FCMNotification
push_service = FCMNotification(api_key=" ... ")
push_service.notify_single_device(registration_id=token, data_message=datamessage, time_to_live=1296000)

When trying to send a notification with long token this is the message I get:

{'multicast_ids': [8149274412512777031], 'success': 0, 'failure': 1, 'canonical_ids': 0, 'results': [{'error': 'InvalidRegistration'}], 'topic_message_id': None}

Upvotes: 1

Views: 1006

Answers (1)

veritas1
veritas1

Reputation: 9170

From the documentation for FirebaseInstanceId:

This class is deprecated. Firebase Instance ID has been replaced with FirebaseInstallations for app instance identifiers and FirebaseMessaging.getToken() for FCM registration tokens.

Looks like you need FirebaseMessaging.getToken() not FirebaseInstallations.getInstance().getToken(true) as you want a FCM registration token.

These APIs provide different tokens for different purposes.

So in your example it would be:

FirebaseMessaging.getInstance()
  .getToken()
  .addOnCompleteListener(new OnCompleteListener<String>() { 
    @Override public void onComplete(@NonNull Task<String> token) { 
    }
  }
 );

Upvotes: 6

Related Questions