Firebase onMessageReceived not called
- tl;dr
Your messages, that are send from somewhere, need to have a data payload and no notification payload, or they won't arrive consistently inside of "onMessageReceived" in your MessagingService.
Notification messages contain a predefined set of user-visible keys. Data messages, by contrast, contain only your user-defined custom key-value pairs. Notification messages can contain an optional data payload.
("Notification messages can contain an optional data payload"
, this will only get submitted to your service, if your application is in foreground!)
Messages created from the console will always be notification-messages!
You can't send plain data-messages, that will consistently arrive at your service through the firebase-console. This is only possible via the REST-API (FCM protocols) or the Admin SDK.
One more important thing:
If you send Notification Messages with a data payload, that you wish to handle, you must handle not only it via the service if the app is in foreground, but also gathering the data from the Intent that your Launch-Activity will receive, which will be opened, if the user clicks the notification! The data payload will be inside of the intent!
- Types of messages:
Notification Messages and Data Messages.
- Notification messages contain a predefined set of user-visible keys. Notification messages have an optional data payload.
If the app is in foreground, they will call
onMessageReceived
(you need to handle the display of the notification on your own)If the application is background, the notification will be displayed (user system tray) and if there is a data payload, it is delivered in the extras of the intent of your launcher Activity.
- Data Messages contain only your user-defined custom key-value pairs. (explained below). And if you want that the message consistently call the onMessageReceived, they should not contain a notification payload, but only a data payload.
From the docs:
Client app is responsible for processing data messages. Data messages have only custom key-value pairs. (! once again - only custom key-value pairs)
You can read about the message types here: Fcm Message Types
- Data Payload
The data payload is a simple json entry, additionally in your payload and can have simple key, value pairs.
Example:
"data": {
"eventId" : "1",
"flavors" : "alpha",
"minFcmVersion" : "3",
"showFallbackOnLowVersion" : "false"
}
Inside of the console, you can add the data payload, if you enter something below the "extended options" field:
Messages created from the console will always be notification-messages!
At first, send a firebase cloud message via the console, directly to your device (use the fcm token) and add a simple data-payload with example values. Now check if your services onMessageReceived is called. (You app must be in foreground!)
Backend
With the REST API you can send Data-Messages, that only contain custom key-value pairs and will always arrive to your service!
Your backend should compose the message itself and add a data payload. This is somewhat described here: HTTP protocol data payload
You can also test a backend wise sent message via one of the several http request builder you find online. I like: hurl.it and
Post
to:
https://fcm.googleapis.com/fcm/send
- Gradle
com.google.firebase:firebase-messaging:20.0.0
<- use this, or higher
That said, there was a bug in versions lower then '11.0.4'
.
- Links and good read
Further good to read documentation here: Receive Messages Documentation And a good explanation on message types as an SOF Answer: SOF - Answer - Message Types