GCM? Not FCM? Google recommends using FCM - so this answer might not be applicable to all aspects but I want to share what I know anyway.
What happens on a device depends on the content of the notification.
Basically you have two types of Notifications:
If the json of the message does NOT contain a "notification"
object (which describes the visible part of the notification in the top toolbar of the device), your App will always receive the notification (wake up), no matter whether it is running, in foreground or background or charging or screen on or off or any other state you can think of. As long as it is connected to the internet of course.
Important is here, that you have registered your service correctly in the manifest and with FCM.
(You need a Service derived from FirebaseMessagingService
that overrides the method onMessageReceived
). I can provide a sample if needed.
If the json does contain a "notification"
object, there can happen two different things:
First, if your app is not running, it will not be started. Instead, just a notification is displayed on the top bar of your device and only when the user clicks it, your app starts.
Second, if your app is running, the Activity that contains the matching intent-filter for the notification will be launched/called.
Here is an example of the json of such a message - notice the tag
- This will group similar messages, and notice the click_action
this is the Action of the intent for the intent filter that will trigger.
{
"to":"<<FCMToken>>",
"priority":"high",
"notification":{
"title":"sender name",
"body":"chat message",
"icon":"icon_nav_main_chat",
"tag":"XMPP_MESSAGE",
"click_action":"XMPP_MESSAGE",
"sound":"default"
},
"data":{
"body":"stanza-content"
}
}
So basically, you decide what will happen by sending the appropriate message.
If you want to receive it ALWAYS (app running or not), you could omit the notification
part, then the message will always be delivered to your service.
The drawback of this is, you have to launch any visible notification locally and put your entire data you need in the payload of the data
object. You have about 4000 Bytes here if I remember right.
But there are use cases where this makes sense. Whatsapp does it that way as far as i know, as they use the way more powerful local notifications with direct reply and that kind of things.
Hope this helps,
cheers, Gris