In my MainActivity
in my log, I can see the token using FirebaseInstanceId.getInstance().getToken()
and it display the generated token. But it seem
Try re-install the app, by uninstall it first from your device or emulator. It will then generate a new token, thus onTokenRefresh() will be called again.
Reinstalling did the trick for me!
Make sure you have added
apply plugin: 'com.google.gms.google-services'
this to bottom of the app.gradle file
I had the same problem like you. My mistake was the following: I placed my <service>
tags outside the <application>
tag 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 problem.
Yes this might happen some 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();
}
I've been struggling with that for over an hour, then I changed the following code and it suddenly 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 both MyFirebaseMessagingService and MyFirebaseInstanceIDService in manifest so it looks like that:
<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 your app, Install again, worked. Tested on real device.
android:exported="true" is a default option so you can also remove that entirely and it will be set to true.
Also if you want to call onTokenRefresh more explicitly, you can simply call that anywhere in your code:
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Localytics.setPushRegistrationId(refreshedToken);
You don't have to rely on broadcast being received any more