I configured a FireBase Cloud Messaging to receive push notifications. All works perfect and the device receives messages.
When my app runs (on foreground) the "MessageArrived" event let me read the message, initialize a notification etc. but, when my app runs in background or is closed, the message is placed on the notification list by Android system without a "MessageArrived" event in my app.
In this case, when I click on the notification my program starts correctly but, how can I read the message text and close the opener notification from the "Activity_Resume" ?
Could you post the parms of the request? It seems to depend on what message type you send. Via console I can reproduce that behaviour (but not with my app) Will check the data structure....
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.
So this means that a message has two parts: The notification itsself AND/OR the data. If you just send a "notification" without data, it is just a notification. If your app is in the foreground, it will receive it. If not, it's just a notification and will be displayed by Android.
If it HAS data, the data will be delivered in the extras of the intent of your launcher Activity.
Clicking on your notification (without data) will start your app but there is no data to fetch.
This was new for me, too. I really thougt a notification is a MESSAGE containing some data and it was the developers job to throw a "real" notification. But it is really an automatic notification. That explains why there is an "icon" and "sound" parameter in the console.
Data messages:
B4X:
Data messages
Set the data key with your custom key-value pairs to send a data payload to the client app. Data messages can have a 4KB maximum payload.
For example, here is a JSON-formatted message in the same IM app as above, where the information is encapsulated in the data key and the client app is expected to interpret the content:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
I modified my HTTP POST request JSON with, as first section, the "notification" and its values (body, title, sound etc.) and then the "data" section with my custom-key values.
Now when I click a notification received with the app in background, my app starts, the notification auto-close it self and in the intent.Extras there are all my "data" custom values !
Many thanks, KMatle and Erel, for your prompt help.