Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM Token is not generating in some android devices [closed]

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?

like image 759
Jd Prajapati Avatar asked Sep 23 '16 10:09

Jd Prajapati


2 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();
like image 69
Arun Dey Avatar answered Oct 26 '22 01:10

Arun Dey


     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));
like image 27
Rjz Satvara Avatar answered Oct 25 '22 23:10

Rjz Satvara