Customising FCM’s automatically generated push notification.

Abhishek Tiwari
3 min readDec 8, 2017

--

This blog is in continuation with my two part series on FCM push notification handling for android.
Reading between the lines — FCM for Android(Part I) &
Reading between the lines — FCM for Android(Part II)
Although an important continuation to third part, I am not including this one to the series because even as a standalone this story serves its purpose.

This blog aims at customising automatic generated FCM notification when app is in background, if you’ve no idea about what i just said please go through the above stories.

Making the background and foreground notifications look the same

The first step would be to make sure both type of messages look the same, i.e. same icon, title, and text.

Foreground Notification: notifications that are handled inside onMessageReceived(), can be easily handled to customise title and text sent as a bundle.

Code inside onMessageReceived to create a notification with “title” and “text” received as a bundle

Background Notification: These are automatic generated notifications, title is set with the value sent from “title” inside advanced options from FCM console and title is set with the value from “message text” option. By default notification icon is same as the app’s launcher icon, also if value inside “title” field in FCM console or in API’s post body is empty, the automatically generated notification will add app’s name as the title.

image showing title and message text fields in FCM console.
JSON equivalent of above sent message from FCM console

How to use custom icon with automatically generated push notification

To do that simply add below lines to your manifest file.

This will make sure your default notification uses your custom icon.

Handling intents with automatically generated push notification

As we know intents inside OnMessageReceived are handled with pending intents, and we can add different activity name as intent routes based upon the key value pair sent as data payload in the notification.
But how to handle the same with, the automatically generated message in the case of app being in background?
The answer is simple, when we click on the automatically generated notification, by default it takes us to the “default” activity in our app.
So the idea is to take start a particular activity on the notification click and handle the intents inside it.
To make an activity as default add this to the activity’s intent filter:

The DEFAULT category is the key

Now on the notification click your app will start from this “default” activity.

Once we’re inside this default activity we can get the data payload as a received bundle to the activity.

Receiving the bundle inside the default activity
getting the string extra “type” which is sent as data payload to set the intent filter accordingly.

So now we will notice the look and behaviour of both the background and foreground notifications are identical so we’re now able to work with “notification” type message which enables us to use FCM console and also work in sync with iOS.

That’ll be all, this has been a long blog series, but with all the uses cases FCM works with, this was as brief as i could’ve gone, Thank You!!

--

--

Abhishek Tiwari

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