Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Messaging not calling FirebaseInstanceId

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?

like image 746
qwertz Avatar asked May 19 '16 15:05

qwertz


People also ask

What can I use instead of FirebaseInstanceId?

FirebaseInstanceId is deprecated but now you can use FirebaseMessaging. getInstance(). token.

How do I get FirebaseInstanceId?

The token can be generated using the following code: FirebaseMessaging. getInstance(). getToken() .

How do I enable cloud messaging on Firebase?

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.

How do you regenerate FCM tokens?

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().


2 Answers

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.

like image 89
Zach Rattner Avatar answered Sep 23 '22 18:09

Zach Rattner


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.

like image 34
Arthur Thompson Avatar answered Sep 21 '22 18:09

Arthur Thompson