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?


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). It will also be called when the token has changed.

According to the FirebaseCloudMessaging 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:

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);



public class FirebaseInstanceIdService extends Service

This class is deprecated. In favour of overriding
onNewToken in FirebaseMessagingService. Once that has been
implemented, this service can be safely removed.

onTokenRefresh() is deprecated. Use onNewToken() in MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public void onNewToken(String s) {

public void onMessageReceived(RemoteMessage remoteMessage) {

Answered By – Prince

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published