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:
Who started the FirebaseMessagingService notification service? There must be some place calling startService(), isn't it?
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 theapp
orservice
isswipe closed/killed/force stop
? Is the relevant behavior documented anywhere?
EDIT:
- I am more concerned about the following case. Let's talk about the
data message
. (compared to thenotification 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 myonMessageReceive()
is not called, becauseMyFirebaseMessagingService
is killed by system and not restarted. Is it possible?
The SDK does this automatically for you.
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
ordata
)) for when your app is in foreground and background.To be able to handle the message yourself (in
onMessageReceived()
), you'll have to senddata
-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.
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: