Home » Android » firebase – FCM Token is not generating in some android devices

firebase – FCM Token is not generating in some android devices

Posted by: admin June 15, 2020 Leave a comment

Questions:

I was having issues in GCM, so i migrates to FCM but I am still getting issues related to token.

I launched my updated app and I checked that there is 300 new users,
200 of them are getting tokens and 100 are not getting any token, it’s a very critical issue.

Any Solutions of null token?

How to&Answers:

You get your FCM token from MyInstanceIDListenerService class onTokenRefresh() method on every fresh installation.

Or you can also get FCM token with this line of code:

FirebaseInstanceId.getInstance().getToken();

Answer:

     class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

            private static final String TAG = "MyFirebaseIIDService";

            @Override
            public void onTokenRefresh() {

                //Getting registration token
                String refreshedToken = FirebaseInstanceId.getInstance().getToken();

                //Displaying token in logcat
                Log.e(TAG, "Refreshed token: " + refreshedToken);

            }

            private void sendRegistrationToServer(String token) {
                //You can implement this method to store the token on your server
                //Not required for current project
            }
        }

     **Sometimes tokens are not generated then write this code**

public class ContCreateTokenService extends Service {
            @Nullable
            @Override
            public IBinder onBind(Intent intent) {
                return null;
            }

            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
                Intent serviceIntent = new Intent(this, MyFirebaseMessagingService.class);
                startService(serviceIntent);
                return START_REDELIVER_INTENT;
            }
        }

public class FetchNewRefreshToken extends IntentService {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */

    public static final String TAG = FetchNewRefreshToken.class.getSimpleName();


    public FetchNewRefreshToken() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        try {

            // Resets Instance ID and revokes all tokens.
            FirebaseInstanceId.getInstance().deleteInstanceId();


            // Now manually call onTokenRefresh()
            Log.e(TAG, "Getting new token");
            FirebaseInstanceId.getInstance().getToken();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Also add it in Menifest file

 <service android:name=".ContCreateTokenService" />
 <service android:name=".FetchNewRefreshToken " />

In splash Activity : –

 startService(new Intent(this, FetchNewRefreshToken.class));
 startService(new Intent(this, ContCreateTokenService.class));