David Truong
David Truong

Reputation: 472

How do I access Firebase tokens outside of the FirebaseInstanceIdService?

I'm upgrading from GCM to FCM. What is the proper way to obtain a Firebase device token outside of onTokenRefresh?

I'm trying to get the device token so that I can re-enable it on my server at a later time. I initially get device token by following the documentation shown here for Method 1. However when I attempt to access the device token directly via method 2 I get a different token. Am I retrieving the device token incorrectly in method 2?

Method 1: inside of FirebaseInstanceIdService.onTokenRefresh()

String refreshedToken = FirebaseInstanceId.getInstance().getToken();

Method 2: Direct access to the device token

FirebaseInstanceId instanceID = FirebaseInstanceId.getInstance();
String registrationToken = instanceID.getToken(this.projectNumber, "FCM");

Upvotes: 5

Views: 2169

Answers (2)

Karue Benson Karue
Karue Benson Karue

Reputation: 1026

For anyone looking for a finer solution to this question, this is how I would approach it. A unique token will be generated if your application runs for the first time or you just cleared application data.

To access token outside of the FirebaseInstanceIdService class, use the SharedPreference inside onTokenRefresh and save your token as shown below.

public class FireBaseTokenClass extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();


        SharedPreferences sharedPreferences = getSharedPreferences("file_save_token", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("myToken", refreshedToken);
        editor.apply();


    }
}

To access your token outside the service class, just say this:

SharedPreferences sharedPreferences = getSharedPreferences("file_save_token", Context.MODE_PRIVATE); 
    String token = sharedPreferences.getString("myToken", "");

Upvotes: 0

AL.
AL.

Reputation: 37768

Just to explain what is happening in Method 1. It is suggested to get the token inside onTokenRefresh() since when this method is triggered, it means that the previous token got invalidated. From the docs:

Called when the system determines that the tokens need to be refreshed. The application should call getToken() and send the tokens to all application servers.

For Method #2, you're calling getToken(String authorizedEntity, String scope), instead of getToken(). The one with two parameters is commonly used when setting up your app to receive messages from multiple senders.

For each call to getToken(String authorizedEntity, String scope) with a different project ID (authorizedEntity) it will return a different token.

Just replace your Method 2 with the same call in Method 1, like so:

String registrationToken = FirebaseInstanceId.getInstance().getToken();

and you should be able to retrieve the designated token. The token is often retrieved in the onCreate() of your initial activity.

Upvotes: 6

Related Questions