ardevd
ardevd

Reputation: 3407

What to do with the INSTANCE_ID_EVENT action with Firebase 17.0.1?

So now that the FirebaseInstanceIdService service is deprecated I'm unsure what to replace it with.

I previously had a service declared in the manifest like so:

        <service
            android:name=".fcm.FcmIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

And the service itself:

public class FcmIdService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {
        // Create notification channel.
        createFcmNotificationChannel();
    }

}

If I remove the service I no longer receive notifications so I'm assuming I have to replace it with something else.

Thanks.

EDIT: This question differs from the proposed duplicate question by also covering the manifest declaration of the affected services which is still unclear to me.

Upvotes: 7

Views: 6400

Answers (2)

Gumby The Green
Gumby The Green

Reputation: 631

All you need in the manifest is this (note that the service doesn't need android:stopWithTask="false" since it defaults to false:

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

And here's the rest of it in beautifully simple Kotlin...

To get the new token whenever it's refreshed:

class MyFirebaseMessagingService: FirebaseMessagingService() {

    override fun onNewToken(token: String?) {
        Log.d("FMS_TOKEN", token)
    }
    ...
}

To get the token from anywhere at runtime:

FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener {
    Log.d("FMS_TOKEN", it.token)
}

Upvotes: 1

Aleksandar Mironov
Aleksandar Mironov

Reputation: 584

Check this topic:

FirebaseInstanceIdService is deprecated

You have to replace it with:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

        @Override
        public void onNewToken(String s) {
            super.onNewToken(s);
            Log.e("NEW_TOKEN",s);
        }

        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
        }
    }

And in manifest:

<service
    android:name=".MyFirebaseMessagingService"
    android:stopWithTask="false">
    <intent-filter>

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

And if you want to get token somewhere in your project:

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this,  new OnSuccessListener<InstanceIdResult>() {
     @Override
     public void onSuccess(InstanceIdResult instanceIdResult) {
           String newToken = instanceIdResult.getToken();
           Log.e("newToken",newToken);

     }
 }); 

There is no need to use

<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />

Hope it helps you

Upvotes: 18

Related Questions