TareKhoury TareKhoury - 1 year ago 3795
Android Question

Firebase FCM force onTokenRefresh() to be called

I am migrating my app from GCM to FCM.

When a new user installs my app, the onTokenRefresh() is automatically being called. The problem is that the user is not logged in yet (No user id).

How can I trigger the onTokenRefresh() after the user is logged-in?

Answer Source

The onTokenRefresh() method is going to be called whenever a new token is generated. Upon app install, it will be generated immediately (as you have found to be the case). I believe it will also be called when the token has changed.

According to the Firebase Cloud Messaging guide:

You can target notifications to a single, specific device. On initial startup of your app, the FCM SDK generates a registration token for the client app instance.


Source Link: https://firebase.google.com/docs/notifications/android/console-device#access_the_registration_token

This means that the token registration is per app. It sounds like you would like to utilize the token after a user is logged in. What I would suggest is that you save the token in the onTokenRefresh() method to internal storage or shared preferences. Then, retrieve the token from storage after a user logs in and register the token with your server as needed.

If you would like to manually force the onTokenRefresh(), you can create an IntentService and delete the token instance. Then, when you call getToken, the onTokenRefresh() method will be called again.

Example Code:

public class DeleteTokenService extends IntentService
    public static final String TAG = DeleteTokenService.class.getSimpleName();

    public DeleteTokenService()

    protected void onHandleIntent(Intent intent)
            // Check for current token
            String originalToken = getTokenFromPrefs();
            Log.d(TAG, "Token before deletion: " + originalToken);

            // Resets Instance ID and revokes all tokens.

            // Clear current saved token

            // Check for success of empty token
            String tokenCheck = getTokenFromPrefs();
            Log.d(TAG, "Token deleted. Proof: " + tokenCheck);

            // Now manually call onTokenRefresh()
            Log.d(TAG, "Getting new token");
        catch (IOException e)

    private void saveTokenToPrefs(String _token)
        // Access Shared Preferences
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();

        // Save to SharedPreferences
        editor.putString("registration_id", _token);

    private String getTokenFromPrefs()
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        return preferences.getString("registration_id", null);