Skip to content Skip to sidebar Skip to footer

Firebase Fcm Token Not Generating

I want to integrate fcm for having push notification functionality in my app. But the problem is my fcm token is not getting generated at all. I used the same code I used in this

Solution 1:

Aside from the implementation errors, have a look at how instance ID works: https://developers.google.com/instance-id/, check the chapter Instance ID lifecycle at the bottom.

I can see 2 points where it might go wrong:

  1. You don't have a connection with the Google servers. Check if you have a working internet connection by opening the device and opening a webpage. Also take proxies and firewalls into account, those might block your traffic (for example, if you are in China, the Great Firewall might block your connection with the Instance ID servers).

  2. Make sure you don't already have a token. This is quite a common error. You implement the ID token service, and run the app. It works fine, now you want to send the token to the server, and write the code for it. When you run the app again, you will see no connection to the server, and think there's an issue with your implementation. What actually happened, is that you already got your token on the first run, and it was cached by the app. The second time it already has a token, and the onNewToken() will not be called. If you uninstall the app, and install it again, it will ask for a new token on launch.

Solution 2:

Finally got the solution to my problem. I got this hint when I was trying to integrate OneSignal notification sdk. The problem was that I had the below code in the application tag of manifest.

tools:node="replace"

This was written in OneSignal docs.

Make sure you are not replacing the tag in your AndroidManifest.xml with tools:node="replace"

As OneSignal was also internally using FireBase I thought to give it a try with the firebase directly and it worked after I removed it.

Hope this will help someone else too

Solution 3:

As said by @Nilesh Rathod FirebaseInstanceIdService is depreciated. So no you need only one service in the manifest.

Try this way. 1. First Create a Service

publicclassYourServiceextendsFirebaseMessagingService {

publicstaticintNOTIFICATION_ID=1;

@OverridepublicvoidonNewToken(String s) {
    super.onNewToken(s);
}

@OverridepublicvoidonMessageReceived(RemoteMessage remoteMessage) {
    Intentintent=newIntent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntentpendingIntent= PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    UridefaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.BuildermNotifyBuilder=newNotificationCompat.Builder(this, "2")
            .setSmallIcon(R.drawable.your_icon)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManagernotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (NOTIFICATION_ID > 1073741824) {
        NOTIFICATION_ID = 0;
    }
    Objects.requireNonNull(notificationManager).notify(NOTIFICATION_ID++, mNotifyBuilder.build());
}

}

Now add this to Manifest

<serviceandroid:name=".YourService"android:exported="false"><intent-filter><actionandroid:name="com.google.firebase.MESSAGING_EVENT" /></intent-filter></service>

Solution 4:

the generate of token for firebase is in first install of app or when you delete the cache for app ,this example of code work for me public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

SharedPreferences sharedPreferences1;
SharedPreferences.Editor editor1;
privatestaticfinalStringPREF_NAME1="prefs_token";
privatestaticfinalStringKEY_FCM="devices_token";
//this method will be called//when the token is generated@OverridepublicvoidonTokenRefresh() {
    sharedPreferences1 = getSharedPreferences(PREF_NAME1, Context.MODE_PRIVATE);
    editor1 = sharedPreferences1.edit();
    super.onTokenRefresh();

    //now we will have the tokenStringtoken= FirebaseInstanceId.getInstance().getToken();
    editor1.putString(KEY_FCM,token.toString());
     editor1.apply();

    //for now we are displaying the token in the log//copy it as this method is called only when the new token is generated//and usually new token is only generated when the app is reinstalled or the data is cleared
    Log.d("MyRefreshedToken", token);
    Stringdevice_token= sharedPreferences1.getString(KEY_FCM, "");

}

}

Solution 5:

The onTokenRefresh/onNewToken methods are only called when a token is generated. Most of the time a token just exists, and is not modified. During that time, onTokenRefresh/onNewToken won't be called.

So most likely, your token was generated when you first add the app, before you had the onTokenRefresh/onNewToken. So to now get the token, you can do two things:

  1. Uninstall and reinstall the app. Deleting the app will delete the existing token, then it will generate a new token on the reinstall, and call your onTokenRefresh/onNewToken.
  2. Also request the token in e.g. your MainActivity.onCreate.

    publicclassMainActivityextendsAppCompatActivity {
    
      @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        ...
        StringiidToken= FirebaseInstanceId.getInstance().getToken();
        Log.d("Firebase", "Got token: " + iidToken);
    

Post a Comment for "Firebase Fcm Token Not Generating"