Reputation: 1738
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 the app
or service
is swipe closed/killed/force stop
? Is the relevant behavior documented anywhere?
EDIT:
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? Upvotes: 27
Views: 15070
Reputation: 179
I tested phones of different brands and got some elementary conclusions.
Let's focus on the Pixel phone first, as it keeps almost the same framework source code. If you build an app without foreground service, you swipe to kill the app, then it can't get any fcm push. But if you build an app with foreground service, you swipe to kill the app, then it has the ability to receive fcm pushes.
That's because swiping to kill app won't kill foreground services, even though all the activities are out of the back-stack. As long as your application keeps alive, then it has the ability to receive fcm pushes.
But if you stop the app forcibly in Settings or uninstall the app, then the application and foreground services would be closed thoroughly. In this situation, the app can't receive any fcm push.
I also tested Xiaomi and Samsung phones. We need to authorize self-launch permission or close battery optimization. Then their apps' foreground services can't be closed by swiping to kill the app. Therefore, their apps can receive fcm pushes.
Upvotes: 1
Reputation: 37778
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 theapp
orservice
isswipe 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.
Upvotes: 26