Using Firebase Cloud Messaging with Android

Firebase Cloud Messaging (FCM) is a free service offered by Google to send and receive cross platform messages. One of the primary example of FCM is Android application exchanging messages or receiving push notifications from a remote server. The notifications can be used to notify the Android Application about a new update or a new feed is available for download. The purpose can be extended to any developer need where the client application is required to be notified of any new event.

Building blocks of FCM

  • Remote Server Application – A remote trusted environment like Firebase GUI or Firebase Cloud Functions or any other server application.
  • FCM Cloud Messaging server – This is Google’s own infrastructure which handles and manages all messaging and exchange of messages between the Client apps and Server application.
  • Client Application – This is the client application like Android, iOS, WEB which receives messages.

FCM Configuration

Android Project Setup

Before we enable Firebase Cloud Messaging in Android project we need to create and link Firebase project. If you are new to Firebase then check out this post on how to connect Firebase project with Android application. Add Firebase Cloud Messaging from the tools menu and add FCM dependencies to your project app’s build.gradle file.

After the above step the Android device is ready to receive push notification.

Android Message targeting

If the goal is to target specific user devices or if the messages needs to be handled by the app then we can extend FirebaseMessagingService service. FirebaseMessagingService provides a function onNewToken which is required to push notification from the remote server application. onNewToken generates registration FCM token that can be sent to your server for targeting user device.

Update build.gradle file to add Retrofit2 and Firebase Auth libraries.

If the purpose is to send messages to this application instance or manage this apps subscriptions on the server side, send FCM Instance ID token to your app server.

In this example I have designed a basic email & password form backed by Firebase Auth. Replace activity layout file with the following xml code

This layout will show a login form and on successful login attempt the app is designed to generate FCM token for the device.

Firebase AUTH for Android


Update AndroidManifest.xml file

Update styles.xml file to support Toolbar.

Let’s see now how to create generate notification token. Call FirebaseInstanceId instance to fetch token.

Activity Class

Custom Message Handling

If you want to handle the notification in the app then you can extend the FirebaseMessagingService class.

Add service class in AndroidManifest.xml to handle new token and messages received.

Firebase Message Sender

We have created our Android application to receive FCM push notifications. Let’s check now how we can use Firebase Cloud Functions to send FCM message. Firebase Admin SDK provides messaging API to send message to group of tokens.

I have explained in my earlier post how to create Firebase Cloud Functions. Follow my guide to create your instance of Firebase function and deploy.

I will show you a basic example of how to use Firebase Cloud functions to send messages to a particular device.

In the above code sendPushNotificationfunction receives FCM token from the Android client and is used to create the message.


Leave a Reply