Android Notification with FCM - Who started the FirebaseMessagingService?

12,313

1. Who started the FirebaseMessagingService notification service? There must be some place calling startService(), isn't it?

The SDK does this automatically for you.

2. My understanding is that, the downstream notification will deliver to my mobile device's google play service, regardless of the state of my app. Then, to what extent can I ensure that my onMessageReceive() is called when the app or service is swipe closed/killed/force stop? Is the relevant behavior documented anywhere?

The official docs for Receiving Message for Android is here. It discusses the behavior of the message (depending on the type of message payload you use (i.e. notification or data)) for when your app is in foreground and background.

To be able to handle the message yourself (in onMessageReceived()), you'll have to send data-only message payloads.

For the topic with regards to swipe closed/killed/force stopped, this topic has been discussed for quite some time and there doesn't seem to be a definite answer. During one of my testings, I am able to still receive a message (tested with a data-only message payload) if I Swipe close my app. But when I force closed it from the Settings menu, I wasn't able to receive any messages. Do note that this is not always the behavior.

There are some devices that were designed that when you swipe close the app, it will be the same as force stopping them (see my answer here).

There are also devices where even if the app is still just simply swiped away, even though it's not force closed, the device itself is preventing it from receiving messages. Others say that this can't be the case because apps like WhatsApp were able to do it. The reason I've learned so far for that is because the device manufacturers have whitelisted most of the well-known apps for it to be possible.

So just to answer your question, no. This is not documented anywhere because (IMO) this is a topic that depends also on the device and that FCM has no total control over.

3. I am more concerned about the following case. Let's talk about the data message. (compared to the notification message.) My client app received the FCM message and shows the default notification, because google play service is running and send it to the system tray. But my onMessageReceive() is not called, because MyFirebaseMessagingService is killed by system and not restarted. Is it possible?

AFAIK, the FirebaseMessagingService is tied with the Google Play Service so so long as Google Play Service is active, so will the Firebase service. A portion from my answer here:

However, I've seen that it was previously mentioned before for FirebaseMessagingService (see this post, comment by @ArthurThompson):

These services will be started by Google Play services, which is always running on the device. You don't have to and should not start/stop these services yourself.

Share:
12,313
Weishi Z
Author by

Weishi Z

Updated on June 05, 2022

Comments

  • Weishi Z
    Weishi Z almost 2 years

    According to the set up guide here, in a sample app, A) I created a class that extends firebase services class. B) I put these classes in the AndroidManifest.xml

    A) Java Class

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            //a) What's the life cycle of the service? How can I ensure this method is getting called?
            //b) If the service is killed at some point, will system restart it on receiving new messages?
            Log.d(TAG, "From: " + remoteMessage.getFrom());
        }
    }
    

    B) AndroidManifest.xml

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

    Then, the app can receive notifications from FCM!

    Here is my question:

    1. Who started the FirebaseMessagingService notification service? There must be some place calling startService(), isn't it?

    2. My understanding is that, the downstream notification will deliver to my mobile device's google play service, regardless of the state of my app. Then, to what extent can I ensure that my onMessageReceive() is called when the app or service is swipe closed/killed/force stop? Is the relevant behavior documented anywhere?

    EDIT:

    1. I am more concerned about the following case. Let's talk about the data message. (compared to the notification message.) My client app received the FCM message and shows the default notification, because google play service is running and send it to the system tray. But my onMessageReceive() is not called, because MyFirebaseMessagingService is killed by system and not restarted. Is it possible?