0

I have tried to search the solutions everywhere, like several links below but couldn't find any correct answer.

FCM token Not Generating

What is FCM token in Firebase?

Firebase FCM force onTokenRefresh() to be called

The problem is every time I tried doing fresh install, both from Android Studio and Play Store, FCM sometimes generates a right token, but sometimes it generarates a wrong token. Whilst the right token is always different, the wrong token is always the same, starting with eyJhb. I know this info is useless since FCM generates the token based on device ID and app ID.

I've forced FCM to generate the token using this code in my LoginActivity.java:

mPrefToken = getSharedPreferences(PREF_TOKEN, Context.MODE_PRIVATE);
fcmtoken = mPrefToken.getString(KEY_TOKEN, "");
if (fcmtoken.equals("")) {
    FirebaseInstallations.getInstance().getToken(true).addOnCompleteListener(new OnCompleteListener<InstallationTokenResult>() {
        @Override
        public void onComplete(@NonNull Task<InstallationTokenResult> task) {
            if(!task.isSuccessful()){
                return;
            }
            fcmtoken = task.getResult().getToken();
            mPrefToken.edit().putString(KEY_TOKEN, fcmtoken).apply();
            sendToken(user_id,fcmtoken);
        }
    });
} else {
    sendToken(user_id,fcmtoken);
}

And in MyFirebaseMessagingService.java

@Override
public void onNewToken(String token) {
    super.onNewToken(token);
    mPrefToken = getSharedPreferences(PREF_TOKEN, Context.MODE_PRIVATE);
    mPrefToken.edit().putString(KEY_TOKEN, token).apply();
}

I'm using the latest Android Studio (4.2.2 and build gradle com.android.tools.build:gradle:4.2.2) but have been experiencing this issue since 4.1.x as far as I can remember. My app is targeting SDK 23 - 30.

I'm using buildToolsVersion "30.0.3" but I believe I've been experiencing this since 30.0.2

FCM version is com.google.firebase:firebase-messaging:22.0.0. Have tried with 21.0.0 but experienced the same thing.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user1801605
  • 441
  • 7
  • 18
  • Starting with `ey` seems to be a JWT, and not an FCM token. I don't think `FirebaseInstallations.getInstance().getToken(true)` returns JWTs, so are you sure those values aren't coming from somewhere else? – Frank van Puffelen Jul 07 '21 at 04:16
  • Yes, I am sure. Because the shared preferences file got written in `onNewToken()` method inside FirebaseMessagingService. Besides I'm not using JWT at all. Just heard about it now. – user1801605 Jul 07 '21 at 08:02
  • We are facing the same issue, did you found a solution? – Hamza Awwad Feb 07 '22 at 17:49

2 Answers2

1

I am having the same issue. Currently it seams that this alternative solution using FirebaseMessaging provides the correct FCM token in my case so far.

    FirebaseMessaging.getInstance().getToken().addOnCompleteListener((r) -> {
        if(r.isSuccessful()){
            String fcmToken = r.getResult();
        }
    });
0

Don't pass .getToken(true) refresh, but .getToken(false) refresh.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216