Firebase Cloud Messaging (FCM) – Push Notification in Android

At I/O 2016, Google introduced an all new & updated version of Firebase, offering a comprehensive solution for creating a back-end infrastructure for mobile and the web.

Initiallly, Firebase was a Mobile-Back-end-as-a-Service (MBaaS) but now Google has transformed it into a complete back-end solution for both mobile and web development. Firebase has one SDK and a console to create and manage applications across Android, iOS as well as the Web. Firebase comes with the following features:

Develop – Build better apps and leave the operations to us. Save crucial development time and ship a high-quality, bug-free app.

  • Cloud Messaging – Deliver and receive messages across platforms reliably
  • Authentication – Reduce friction with robust authentication
  • Realtime Database – Store and sync app data in realtime
  • Storage – Store files with ease
  • Hosting – Deliver web content faster
  • Remote Config – Customize your app on the fly
  • Test Lab – Test in the lab, not on your users
  • Crash Reporting – Keep your app stable

Grow – Acquire and engage the right users at the right time. Take the guesswork out of growth.


Get Started with Firebase console:

firebase select project type

Open Firebase Console and create a new project.



Enter your app name and select your country and press the create project button.



Enter the package name of your project in which you are going to integrate the Firebase.



Here the google-services.json file will be downloaded and place it in the root director of your project.

Note: This step is very important as your project won’t build without this file.



Add Dependencies to Gradle file,

project-level build.gradle

app-level build.gradle

Available libraries

These libraries are available for the different Firebase features.

Gradle Dependency Line Service Analytics Realtime Database Storage Crash Reporting Authentication Cloud Messaging / Notifications Remote Config Invites / Dynamic Links AdMob App Indexing


Well we are not going to discuss about all the available libraries, only Cloud Messaging service in this post.


So lets starts with Cloud Messaging Service:

To write your Firebase Cloud Messaging Android client app, use the FirebaseMessaging API. The instructions in this page assume that you have completed the steps for adding Firebase to your Android project.

Set up Firebase and the FCM SDK

In Android Studio, add the FCM dependency to your app-level build.gradle file:

Get Firebase Instance

To subcribe or unsubcribe a topic, create a shared instance of the FirebaseMessaging object.

To Subscribe a Topic

To Unsubscribe a Topic

Create Service to get Registration Token

Create aservice that extends FirebaseInstanceIdService to handle the creation, rotation, and updating of registration tokens. This is required for sending to specific devices or for creating device groups. This class is optional, required only if need to access the token.

Create Service to Receive Messages

Create a service that extends FirebaseMessagingService. This is required if you want to do any message handling beyond receiving notifications on apps in the background. To receive notifications in foregrounded apps, to receive data payload, to send upstream messages, and so on, you must extend this service.

Edit your app manifest

Add the following to your app’s manifest:


For FCM to work, we need request for internet permission.

Access the device registration token

On initial startup of your app, the FCM SDK generates a registration token for the client app instance.

Retrieve the current registration token

When you need to retrieve the current token, call FirebaseInstanceID.getInstance().getToken(). This method returns null if the token has not yet been generated.

The registration token may change when:

  • The app deletes Instance ID
  • The app is restored on a new device
  • The user uninstalls/reinstall the app
  • The user clears app data.

Monitor token generation

The onTokenRefresh callback fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token.


Create Notification to Display the Message

Now lets starts with Notification Section:

On your project dashboard. Find the Notification section or console at left menu bar and click New message or Send your first message (If you didn’t send any message yet).


Enter your message, select your app, enter title in advanced options and click on the Send Message button and check your device for the notification.


Sample Services:


build.gradle (app-level)



I hope you will like the article and it will definitely help you to make your apps more productive. In case of any confusion or query, feel free to ask. And if you found this tutorial to be useful, Share with others.

About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.