Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Can an Android app have multiple FirebaseMessagingServices

I am building a library project in which I have a FirebaseMessagingService. I also have a FirebaseMesagingService in my app. What I see is, whenever an FCM is sent from the server, only one FirebaseMessagingService processes it. This was not the case when I was using GCM Receivers. Both the GCM Receivers used to receive the message and based on message content, they would/would not do anything about it.

How can I achieve the same in FCM.

like image 427
Kanika Avatar asked Dec 05 '17 17:12

Kanika


People also ask

What is Firebasemessagingservice?

Firebase Cloud Messaging Platform (formerly named as GCM) is a free mobile notification service by Google that enables (third-party) app developers to send notifications from GCM (Google Cloud Messaging) servers to their users.

How does FCM work in Android?

The FCM backend receives the message request, generates a message ID and other metadata, and sends it to the platform specific transport layer. When the device is online, the message is sent via the platform-specific transport layer to the device. On the device, the client app receives the message or notification.


3 Answers

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

like image 90
temnoi Avatar answered Sep 17 '22 16:09

temnoi


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.

like image 22
Kanika Avatar answered Sep 20 '22 16:09

Kanika


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

like image 33
Umang Avatar answered Sep 18 '22 16:09

Umang