Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Notification with FCM - Who started the FirebaseMessagingService?

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?
like image 947
Weishi Z Avatar asked Mar 30 '17 21:03

Weishi Z


People also ask

What is FirebaseMessagingService service Android?

public class FirebaseMessagingService extends Service. Base class for receiving messages from Firebase Cloud Messaging. Extending this class is required to be able to handle downstream messages.

How is FCM token generated?

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. This is the token that you must include in targeted send requests from the API, or add to topic subscriptions for targeting topics.

What is Android FCM name?

Firebase Cloud Messaging (FCM), formerly called Google Cloud Messaging (GCM), is a free cloud service from Google that allows app developers to send notifications and messages to users across a variety of platforms, including Android, iOS and web applications.

Who is FCM Googleapis?

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost.


1 Answers

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.

like image 104
AL. Avatar answered Sep 28 '22 01:09

AL.