Reading between the lines — FCM for Android(Part I)

Abhishek Tiwari
5 min readDec 4, 2017

--

FCM(Firebase Cloud Messaging), as we all know is a cross-platform cloud messaging service, that is largely used for enabling push notification to the app.

To set up FCM to your android app you can follow the documentation which is fairly simple, I won’t divulge into the setup because the main aim of this blog is to cover the problems we usually face with FCM.
Hence, the prerequisites here would be that you have FCM set up and you are receiving a message on your phone from firebase console.

If you already know the drill around FCM and is stuck on how to handle automatically generated message’s click action please check my answer at StackOverflow, it is basically the same post , but quite briefed.

Handling FCM Tokens!

Q. what is a FCM token, what am i supposed to do with it?
A. when we successfully setup firebase to the app and install it to a device, a unique FCM token is generated for that particular device. These tokens are used when you have to send a push notification to a single device.

Q. How to access FCM token?
A. when we first install the app(which has FCM set up), OnTokenRefreshed method inside MyFirebaseInstanceIDService is called where you can get the token and save it in your SharedPreference. The point to note here is that onTokenRefreshed will only be called when the app is first installed to a device and not when :-
a)App’s data is cleared or
b)App is updated with FCM to an existing app where FCM was not set up.

In order to get the device token at any given time you can call FirebaseInstanceId.getInstance().getToken() .

“I was Receiving Notification even when i didn’t want to” — me, some time ago!

So i was receiving a notification on my device when a notification was fired from FCM console even when i had no code for notification generation in OnMessageReceived method and there was no apparent solution to this question anywhere on stackoverflow too.
The Problem was that, firebase handles notifications differently depending upon the app being in foreground or background.

Q. What does foreground and background means ?
A. Quite simply, firebase’s message, when fired from console is received inside OnMessageReceived method, only when our app is in foreground, i.e. we are currently using our app on the device. On the other hand OnMessageReceived is not called if our app is paused (basically our app is not being used), in this case an automatic notification is fired from firebase.

Types of messages, Data, Notification & Notification with data payload!

This is the most important part of the discussion, there are two types of message that is sent from FCM, Data message and Notification message.

The push notification sent from the FCM console is a “Notification” type message.

To talk about the difference between these three type we need to choose a common ground to talk about, irrespective of the fact that the message is sent from console or through FCM API, firebase handles the messaging based of JSON body of the request message containing the title, body and data payload(if available). JSON body of a “notification” type message looks like this:

Notification message

The above JSON body is an equivalent of a console composed message like this:

Now let’s suppose we need to send additional fields to the devices along with the notification!(Notification message with data payload is used)
In that case we send additional fields from the advanced options in the console like this:

Notification message with data payload

The equivalent JSON body will look like this:

JSON equivalent of Notification message with data payload

JSON body of a “Data” message looks something like this:

JSON body of a Data message

Currently a message sent from FCM console only sends either a notification message or a notification message with data payload, in order to send a data message, we need to hit FCM API with data message JSON as post body.

Why to bother about these different types of messages?

Q. Exactly, why do i need to know about notification or data message?
A. The primary reason is to solve the problem of automatically generated messages in android! But How?

“When we send a push message from FCM console we get automatically generated notification when app is in background, and notification created from inside OnMessageReceived, when app is in foreground” — I know that already!

The question is why that happens!!!
This is because FCM console creates a “Notification” type message, and the whole scenario of foreground and background notification exists because we are sending a notification type message(same with notification message with data payload).
But apparently, this problem will cease to exist if we send a “data” message, i.e. a data message is always intercepted by OnMessageReceived method, irrespective of the app being in foreground or background.

A Data type message is always intercepted by OnMessageReceived irrespective of the app being in foreground or background!

So, basically our problems is solved and we can now avoid generating automatic push notification.
But using a “Data” type message has it’s own drawbacks,
for ex. if your server is hitting FCM notification api with a “Data” type JSON body as post parameter(This is the only way to send a data message to your app), so that a push notification can be shown on the apps, You will realise that although notification is generated on android app but no notification came on iOS.

I will discuss why that happened, and how to make things work even with a notification message and how to handle the automatically generated notification in the next part!

--

--

Abhishek Tiwari

Android developer. Python enthusiast. IoT newbie.Open Source Contributor.Anime buff.