Android: Can an Android app have multiple FirebaseMessagingServices

The android app cannot have multiple FirebaseMessagingService as its a service not a receiver. What one can do, is to check two conditions:

  1. If app has FCMMessagingService registered in its functionality, then provide a method in the library project which can take the message as parameter, received by app's FCMMessagingService.

  2. If the app does not have FCM functionality integrated in it, then have FCMMessagingService in your library project which can handle the fcm sent by the server.


Your library's manifest should not have a FirebaseMessagingService subclass. Adding the messa service to the app's manifest should be a part of the integration step, integrating the SDK. Also you should provide a hook in the SDK from where the app can pass the FCM message payload to the SDK.

Essentially if the app does not have its own FirebaseMessagingService subclass it would add your SDK's listener service in the manifest else it will add the hook in its own listener service which passes the payload to your SDK and SDK takes the required action


I've got the same issue a few days ago and in our team, we used a slightly different approach which involves reflection.

In general, we use delegate class and provide context via reflection.

public class OwnPushService extends FirebaseMessagingService {

    private List<FirebaseMessagingService> messagingServices = new ArrayList<>(2);

    public GCPushService() {
        messagingServices.add(new AirshipFirebaseMessagingService());
        messagingServices.add(new TLFirebaseMessagingService());
    }

    @Override
    public void onNewToken(String s) {
        delegate(service -> {
            injectContext(service);
            service.onNewToken(s);
        });
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        delegate(service -> {
            injectContext(service);
            service.onMessageReceived(remoteMessage);
        });
    }

    private void delegate(GCAction1<FirebaseMessagingService> action) {
        for (FirebaseMessagingService service : messagingServices) {
            action.run(service);
        }
    }

    private void injectContext(FirebaseMessagingService service) {
        setField(service, "mBase", this); // util method for recursive field search
    }
}

I've written an article on Medium about this approach if you are interested in the details: link