Understanding the Different Types of FCM Messages

Photo by Museums Victoria on Unsplash

In this blog post, we will simplify the different types of messages that can be received by your Android applications using Firebase Cloud Messaging (FCM). Knowing how the Firebase SDK handles these message types can save you time debugging and improve your app’s functionality. If you need help integrating FCM, the official documentation provides a step-by-step guide (https://firebase.google.com/docs/cloud-messaging/android/client). Let’s get started.

There are two different states the app can be in from the point of FCM messages; foreground and background. Based on the app state the FCM messages behave differently. We’ll see the matrix that’ll simplify the overall behavior of the messages so for now knowing the app state is going to be in that matrix is enough.

Photo by Kristina Flour on Unsplash

This is the simple one. The main purpose of this message type is to simply pass along some …drumroll please… data. The firebase SDK will simply call the onMessageReceived method so you will have the chance to handle the data you receive.

The reason I think this is the simple one is it will always call the onMessageReceivedmethod regardless of the app state.

Here is a sample object from the official page to have a clearer view of what this data message is;

The code you see above is from the server side that creates a message. You can include as many key-value pairs in the data as you’d like, as long as the total size is less than 4000 bytes. The naming of the pairs is up to you, or your backend developer colleague.

Hold on a second. Don’t we use the FCM messages only to display notifications already?

Photo by Ben White on Unsplash

Not necessarily. You can do other stuff like updating some data as we’ve already seen on the “Data messages”, or basically whatever you want.

Notification messages are specifically for displaying notifications though. Let’s see the server-side sample code to create a notification message.

The title and body are fields that are used to generate the notification that’ll be handled by the firebase SDK. Your android (or basically any other client) application will be aware of the push event when the user clicks on the notification IF the app state is background. Otherwise, the event will trigger the onMessageReceived method call.


Starting with the sample code;

So, this shouldn’t be a surprise here hopefully. But why?

But why?

This is actually pretty handy when you have extra information you want to pass to your push notification but you don’t want to care about the push notification creation step.

Just consume the data key-value pairs from the extras as if they are passed from another activity with a bundle.

Lastly, do not forget to handle the onMessageReceived method since it’ll be called if your app is in the foreground.

In “The Definitive Guide to Firebase: Build Android Apps on Google’s Mobile Platform” the author covers Firebase Cloud Messaging (FCM) in detail. As an Android developer, understanding FCM is crucial to being able to effectively engage with your app’s users. Chapter 9 (Firebase Cloud Messaging) of the book discusses the different types of messages that I’ve tried to explain above and also covers options for targeting messages, including using topics or device groups. By reading this chapter, Android developers will gain a solid understanding of FCM and how to use it to enhance the user experience in their app. Additionally, the chapter provides best practices for handling messages in your Android app, which can help developers avoid common pitfalls and ensure that their app is efficient and reliable.

**Amazon Affiliate links are used in this post.
* The kindle edition can be found at the following link;
The Definitive Guide to Firebase: Build Android Apps on Google’s Mobile Platform



Android Developer @Teckro.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store