The simplest and most flexible solution is to extend the FirebaseMessagingService and handle the notification yourself. But first, instead of using notification on your notificationContent in your cloud function, you should change this to data so that you send a data message instead of a notification message. The difference is that the notification message will have an implicit collapse key (application package name), while the data message will not have it. But the data message must be processed on the client, otherwise it will not be displayed.
Here is an example of what you will need for a FirebaseMessagingService :
public class MyFCMService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getNotification() != null) {
PS. When you send a notification from your cloud function (well, if you use the data tag, this is actually a data message, not a notification message), then this method will be called, regardless of whether the application is in the background or in the foreground. HOWEVER , when you send a notification from the firebase console, this method will be called ONLY if the application is in the foreground. If the application is in the background, the Firebase SDK will process the notification and show it to the user. In some cases, it makes sense to show a notification only when the user does not start the application, for example, if you want to advertise some new features of the application. In this case, you can use a unique tag in the notification console (for example, "display_in_foreground") and check this tag on the client. If you set it to true, you can show the notification even to users who are currently running the application, or if it is false, you can skip the notification. This check will only be performed if the application is in the foreground. If it is in the background, it will not be called at all, and the SDK will process to show a notification.
source share