Solution 1 :
FCM documentations states that
With FCM, you can send two types of messages to clients:
Notification messages, sometimes thought of as “display messages.” These are handled by the FCM SDK automatically.
Data messages, which are handled by the client app.
In your case, it seems that you are looking for Notification messages
. In short, FCM will automatically display a standard android notification with a title and a message (body
), when your app is in background.
To get this to work, the notification needs to be sent through Firebase (for example using the firebase console or cloud functions) and it needs to contain the notification
key like in the following sample:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
For what regards your app’s side, it mainly just needs to be setup as a Firebase client. If in any case you want to receive/display the notification also when your app is in foreground, then you’ll have to create a service that extends FirebaseMessagingService
as explained here
Bonus: since it looks like your target is android 8+, you may also want to look at Notification channels as every notification must be assigned to a channel in order to be displayed. By default, you’ll get one with basic settings.
Problem :
Due to numerous app restrictions by android including Doze Mode and different OEM having their own implementation of battery optimization.
So I found out that GCM services is always running, is there a way for FCM to handle notification in background on its own?? Without depending on my app or app state?
I only send notification message which I want the FCM to handle without my app, because my app might be forced stopped or killed by app killers.
Can FCM just handle notification without my app’s involvement?
Comments
Comment posted by firebase.google.com/docs/cloud-messaging/concept-options
What would you like FCM itself to do with the notification exactly? If the behavior you want is only for a notification to be displayed, that’s a “display message” and FCM should be able to display it for you even when the app is killed (given your notification contains the expected keys). More on that here
Comment posted by Jason
Pls explain : given your notification contains the expected keys
Comment posted by firebase.google.com/docs/cloud-messaging/…
The documentation will surely explain it better than I 😉 have a look at
Comment posted by Frank van Puffelen
That sounds like the start of a good answer @Marino! 🙂
Comment posted by Marino
Good point @FrankvanPuffelen! Added some more details below, any correction would be welcome! 🙂
Comment posted by Jason
I understand everything you’ve said. But u didnt answer one question: can FCM handle my message even if my app is force stopped?
Comment posted by Marino
Glad to hear that @Jason! If this ends up being enough to solve your problem, you can set the answer as accepted so people knows this is solved! Otherwise you can add more details to the question to get help on any further issues 🙂
Comment posted by Marino
FCM will handle the notification if the app is killed normally (closed). As far as I know, that’s also what most “app killers” or memory cleaners would do, it’d be weird if they force killed it as any app may misbehave when doing that. I don’t think FCM would be able to receive notifications if the app have been force killed, but I may be wrong here. Guess the easiest way to know is setting it up and giving that a shot 🙂 if you do, I’d be interested of hearing whether it worked out