Issue
In a previous question I was facing the problem, that firebase messages are not triggering the onMessageReceived() event in my firebase class within my app, while the app is in the background. As this friendly user pointed out, the reason seems to be, that the firebase console can't send data messages, it only sends notification messages which won't reach the event. He suggested using postman to send data messages to my app. I set up a POST-request to https://fcm.googleapis.com/fcm/send and added the API-key of my project as well as the content type to the Headers-section like this:
Authorization = key=[firebase project key]
Content-Type = application/json
and I addressed my app with the help of its firebase-token:
{
"to": "[token]",
"notification": {
"title": "This is a title"
"body": "This is a test"
},
"data": {
"title": "This is a data title"
"body": "This is a data message"
"key1": "value1"
}
}
The version I've shown you works just like the firebase console messages. It reaches the app correctly when in foreground (displaying the contents of the notification-section) and transmitting the data. But when in background it reaches the app but without triggering the onMessageReceived() event. I tried to cut out the notification section to make the message a "pure" data-message, but in this case the message won't reach my app at all. Does somebody know what I'm doing wrong?
(I can tell that it does not reach the onMessageReceived event because within there I'm giving the notification a different logo for test purposes)
Solution
Obviously it is because your notification part is null and you only execute the notification builder unless it is not.(as to the code from previous question)
if (remoteMessage.getNotification() != null ) {
//if you send a data message only, this part here will never execute.
}
You can modify above to below and try.
if (remoteMessage.getNotification() != null || remoteMessage.getData() !=null ) {
//This will execute when either notification or data message is not null
}
Answered By - Anjana
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)