One important thing you may have missed in FCM Android

You might have integrated FCM in your app and have collaborated with backend developers to send you notification, but you may be missing one important thing which may be not your problem but still you and the backend developer should know. There is one subtle thing which can pull out your hairs and you and the backend developer are fighting to prove who is correct.

This thing happened with me so I want to share it with all. One day there were some changes in notification display and I wanted to configure my notification code. I found a strange thing happening. I tried to find out what was happening and I tried to remove all of my code for notification generation. But still there was notification displaying. I completely removed all of the code in onMessageReceived(), but still notification was displaying. STRANGE !!! Then I googled it and found that the answer was written in the documentation which I had never read before.

The link is here. I will try to explain what it says.

{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "This week's edition is now available.",
"title" : "NewsMagazine.com",
"icon" : "new"
},
"data" : {
"volume" : "3.21.15",
"contents" : "http://www.news-magazine.com/world-week/21659772"
}
}

The server can send the notification as above in a JSON format. The notification json object contains the notification to be displayed. If there’s more data to be send which the client app will process, it is send in data object.

There are two cases when notification comes- your app is in foreground or in background. The handling is done as follows:

Thus if your app is in background and a notification comes, the notification is generated by the System and not you(in the onMessageReceived() method). This was the problem I was facing when I commented all my code and still notification was shown. Thus, I got to know what was happening.

Source: www.giphy.com

Thanks for reading the article. Suggestions/ Corrections/ Comments are always welcomed. If you like it, please hit the like button and share the article with the Android community. Let’s share the knowledge as much as we can.

Also, Let’s become friends on About.me, Twitter, LinkedIn, Github and Facebook.