I just updated GCM to FCM according to this guide here. The site says my service which extends FirebaseInstanceIdService will be called as soon as the token is generated. For some reason, my service is never called and onTokenRefresh
is never executed.
My services are registered like this, according to the guide:
<service android:name=".fcm.FcmService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".fcm.InstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCEID_EVENT"/>
</intent-filter>
</service>
My service:
public class InstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "InstanceIdService";
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
Log.e(TAG, "Got token: " + token);
}
}
The LogCat doesn't say anything about missing configuration files or else, and I can see logs about FirebaseApp and a Log entry mentioning the token, but still my service is never called.
Do I need to start this service somewhere in my activity? Am I doing something wrong? Can someone guide me in the correct direction?
EDIT: using Log.e("TOKEN", "Token: " + FirebaseInstanceId.getInstance().getToken());
correctly prints my token, so that means it was generated successfully. The service still doesn't get called...
EDIT 2: Changing INSTANCEID_EVENT to INSTANCE_ID_EVENT fixed the issue, although I had to reinstall the app. As I already released a beta version containing the new FCM, my beta testers won't be able to subscribe to the topics to reveice incoming messages. So how should I solve that?
FirebaseInstanceId is deprecated but now you can use FirebaseMessaging. getInstance(). token.
The token can be generated using the following code: FirebaseMessaging. getInstance(). getToken() .
Go to the APIs & auth / APIs section and enable Google Firebase Cloud Messaging for Android. Go to the APIs & auth / Credentials section and click the Create New Key button. Click the Server key button. Impose restrictions (if any), click the Create button.
getToken() again, get a new token, and send that up to the server (probably including the old token as well so your server can remove it, replacing it with the new one). So deleting the FirebaseInstanceId will refresh the token, thanks! after GCM to FCM, FirebaseInstanceId. getInstance().
Thanks Endanke for figuring this out with me. It seems that the Firebase documentation is inconsistent. One page says your intent filter in your manifest file should be INSTANCE_ID_EVENT
, and another says INSTANCEID_EVENT
.
You should use INSTANCE_ID_EVENT
, and once you make the change, you'll need to completely uninstall and reinstall your app for the changes to kick in.
FCM should be used as a replacement to GCM. In your case it looks like you have services for both FCM and GCM in your manifest. Try remove the gcm.GcmService, and only use the one that extends FirebaseInstanceId.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With