I know that there is the same question with this title but unfortunately it is not answered right and it is accepted!!! here
I want to know how I can find out a FCM message received when app is in background to do some action on message received before clicking by user. but when app is in background onMessageReceived
is not triggered!
I googled so much and could not find a good way.
As I've mentioned in the comments section, when sending a message with a notification
message payload the Android System (Notification Tray) will handle the push notification when your app is in background.
You should use a data
-only message payload, so that it will always be handled by onMessageReceived()
.
See the Handling Messages docs in Android for more details.
To handle FCM push notification from onMessageReceived()
when the app in background the server should always send Data only messages.
Notification messages can only handle when the app is in the foreground. When the app is in the background an automatically generated notification is displayed. When the user taps on the notification they are returned to the app. Messages containing both notification and data payloads are treated as notification messages.
With FCM, you can send two types of messages to clients:
- Notification messages, sometimes thought of as "display messages."
- Data messages, which are handled by the client app.
A notification message is the more lightweight option, with a 2KB limit and a predefined set of user-visible keys. Data messages let developers send up to 4KB of custom key-value pairs. Notification messages can contain an optional data payload, which is delivered when users tap on the notification.
Notification messages
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Notification messages are delivered to the notification tray when the app is in the background. For apps in the foreground, messages are handled by these callbacks:
onMessageReceived()
on Android. The notification key in the data bundle contains the notification.
Data messages
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
On Android, a client app receives a data message in onMessageReceived()
and can handle the key-value pairs accordingly.
Note these further platform-specific details:
- On Android, the data payload can be retrieved in the Intent used to launch your activity.
Messages with both notification and data payloads
{
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
App behavior when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground—essentially, whether or not it is active at the time of receipt.
When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
When in the foreground, your app receives a message object with both payloads available.
Reference
- About FCM messages
- Handling received messages
Handle notification messages in a backgrounded app
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.
This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.