Firebase onTokenRefresh () is not called

In my MainActivity in my log, I see a token using FirebaseInstanceId.getInstance().getToken() and displays the generated token. But it seems that in my MyFirebaseInstanceIDService , where it continues until FirebaseInstanceIdService , onTokenRefresh() not called, where in this function it says that the token is originally generated here. I needed to call sendRegistrationToServer() , so I'm trying to understand why it is not included in onTokenRefresh() .

Here is my code

 public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); sendRegistrationToServer(refreshedToken); } } 
+69
android firebase firebase-cloud-messaging firebase-notifications
May 26 '16 at 4:05
source share
13 answers

onTokenRefresh in FirebaseInstanceIdService is called only when a new token is created. If your application was previously installed and a token was generated, then onTokenRefresh will not be called. Try uninstalling and reinstalling the application to force the generation of a new token, this will cause an onTokenRefresh call.

Also make sure your FirebaseInstanceIdService is correctly defined in your AndroidManifest.xml

In your manifest file.

  <service android:name="com.bnt.etailers.fcm.MyFireBaseInstanceIDService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> <service android:name="com.bnt.etailers.fcm.GCMNotificationIntentService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> 

Class FirebaseInstanceIdService

 public class MyFireBaseInstanceIDService extends FirebaseInstanceIdService { private static final String TAG = MyFireBaseInstanceIDService.class.getSimpleName(); @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); if (refreshedToken!=null) { SettingPreferences.setStringValueInPref(this, SettingPreferences.REG_ID, refreshedToken); } // TODO: Implement this method to send any registration to your app servers. sendRegistrationToServer(refreshedToken); } // [END refresh_token] /** * Persist token to third-party servers. * * Modify this method to associate the user FCM InstanceID token with any server-side account * maintained by your application. * * @param token The new token. */ private void sendRegistrationToServer(String token) { // Add custom implementation, as needed. }} 

Class FirebaseMessagingService.

 public class GCMNotificationIntentService extends FirebaseMessagingService { // Sets an ID for the notification, so it can be updated public GCMNotificationIntentService() { super(); } @Override public void onMessageReceived(RemoteMessage message) { }} 
+101
May 26 '16 at 15:47
source share

I had the same problem as you. My mistake was this: I placed the <service> tags outside the <application> in the AndroidManifest.xml file.

Now mine looks like this:

 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> </application> 

And it works without any problems.

+23
May 26 '16 at 13:28
source share

Yes, this can happen several times.

Please call the following method wherever you want to get your fcm id.

  try { String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d("Firbase id login", "Refreshed token: " + refreshedToken); } catch (Exception e) { e.printStackTrace(); } 
+14
Nov 09 '16 at 5:56
source share

In my case, I forgot to add Internet permission to the manifest,

 <uses-permission android:name="android.permission.INTERNET" /> 

I hope that most people will not make this mistake.

+13
Nov 25 '16 at 10:15
source share

Try reinstalling the application by uninstalling it first from your device or emulator. Then it will generate a new token, so onTokenRefresh () will be called again.

+13
Nov 29 '16 at 7:27
source share
 try { FirebaseInstanceId.getInstance().deleteInstanceId(); } catch (IOException e) { e.printStackTrace(); } 

in onCreate to remove the token.

+4
Oct 22 '16 at 7:14
source share

Make sure you add

apply plugin: 'com.google.gms.google-services'

this is at the bottom of the app.gradle file

+4
Apr 12 '18 at 12:06
source share

In my case, I tried everything, but onTokenRefresh never called. Then I change my WiFi and connect to another network , now it worked !!. The previous WiFi has a good internet connection, I don’t know why this is happening. Maybe this can help someone.

+3
Aug 01 '17 at 13:35 on
source share

Reinstalling did the trick for me!

+3
Nov 14 '17 at 17:52
source share

I found that MyFirebaseInstanceIDService is not called when you launch the application on a device where the Internet connection is not available. Therefore, onTokenRefresh () is not called. Therefore, make sure that your device must have an Internet connection while the application is running to receive an updated FirebaseToken.

Also uninstall the previous application and reinstall it to get an updated token. The registration token changes when:

1) Application removes instance id

2) The application is restored on the new device

3) User uninstalls / reinstalls the application

4) The user clears the application data.

+3
Mar 21 '18 at 8:36
source share

There are several reasons not to call the onTokenRefresh method not called. I listed it and mentioned it one by one.

  1. Please make sure you have added Internet permission.

  2. check if you added the google-services.json file that is generated from the Google console in the application directory in your project

enter image description here

  1. In your module’s Gradle file (usually app / build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:
 apply plugin: 'com.android.application' android { // ... } dependencies { // ... implementation 'com.google.firebase:firebase-core:16.0.4' // Getting a "Could not find" error? Make sure you have // added the Google maven respository to your root build.gradle } // ADD THIS AT THE BOTTOM apply plugin: 'com.google.gms.google-services' 

4) add the path to the play service class to the project level build.gridle file

 buildscript { // ... dependencies { // ... classpath 'com.google.gms:google-services:4.1.0' // google-services plugin } } allprojects { // ... repositories { // ... google() // Google Maven repository } } 

5) Make sure you add these services to the AndroidManifes file inside the application tag.

 <service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> </application> 

6) The onToken update is called when the token is updated and during the first installation of the application, so make sure you uninstall the application manually or delete the data.

7) Make sure you have a FirebaseMessagingService in your class of service

[ Note : if it doesn’t work only for a specific version, for example, for the Android KitKat version, try changing the FCM version.]

+3
Oct 26 '18 at 7:52
source share

I struggled with this for more than an hour, then I changed the following code and it unexpectedly worked.

handle your refreshedToken as such:

 String refreshedToken; try { refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); Localytics.setPushRegistrationId(refreshedToken); } catch (Exception e) { Log.d(TAG, "Refreshed token, catch: " + e.toString()); e.printStackTrace(); } 

Try adding android: exported = "true"> to MyFirebaseMessagingService and MyFirebaseInstanceIDService in the manifest so that it looks like this:

 <service android:name="com.localytics.android.sample.fcm.MyFirebaseMessagingService" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <service android:name="com.localytics.android.sample.fcm.MyFirebaseInstanceIDService" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> 

Uninstall the application, install again, it worked. Tested on a real device.

android: exported = "true" is the default option, so you can also completely remove it and it will be set to true.

Also, if you want to call onTokenRefresh more explicitly, you can just call it anywhere in your code:

 String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Localytics.setPushRegistrationId(refreshedToken); 

You no longer need to rely on broadcasting

+2
Mar 23 '18 at 17:00
source share

I have the same problem. My device (kitkat 4.4.2) onTokenRefresh() for some reason failed onTokenRefresh() . I had an excellent Internet connection, the Google Play services were updated and all the necessary conditions for Firebase to work were met. My code worked fine on devices 5.0.0 and above. To solve this problem, I had to reset the device to factory settings, and the application started working. I know this is a tough measure, but maybe it can help someone. There must be some problem or conflict with another application, which is why onTokenRefresh() not called. Good luck

+2
Apr 17 '18 at 13:28
source share



All Articles