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.
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.
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.
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
The android app cannot have multiple FirebaseMessagingService as its a service not a receiver. What one can do, is to check two conditions:
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.
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
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