Android Push Notifications using Firebase Cloud Messaging (FCM)

Android Push Notifications using Firebase Cloud Messaging (FCM)

Firebase Notification is a free service by Google for sending messages to mobile devices using Firebase Cloud Messaging(FCM). FCM provides an well maintained platform to send notifications that appears on the system tray of the devices. In this article we will discuss about the implementation of FCM in android devices.

Firebase Setup

If you are using Android Studio(version>2.2 or later) we will recommend using Firebase Assistant. Still it can be added manually. We will discuss both of them.

Using Firebase Assistant

  • Go to Tools from menu bar.
  • Click Firebase. Assistant window will open on the right hand side with different options like Analytics, Cloud Messaging, Authentication etc.
  • Select Cloud Messaging.
  • And then Set up Firebase Cloud Messaging.

After all the above steps you completed you will find 4-5 actions/steps there like Connect to Firebase, Add FCM to your app, Access the device registration token, Handle messages etc. which we will elaborate while discussing “adding Firebase manually”.

Add Firebase to your app manualy

Setting up Firebase from Firebase Assistant is very easy as we saw in the last paragraph. Still you can add it manually. To achieve it manually, you need to go through the following instructions

  • First of all, you need to create a Firebase project from Firebase Console. Or you can or import a Google project if you already have one.
  • Then click to ‘Add Firebase to your app’.
  • It will ask you to create or select package name for your app.
  • Next you need to download configuration file. A google-services.json file will be downloaded.
  • After downloading the google-services.json file copy it to your project’s app folder(module).

Firebase is set up. Next we will integrate the SDK in your app.

Integrate SDK

Go to build.gradle(root level) and add the following rule

Next you need to add the following depedencies in your app level build.gradle file.

Sync your Project and make sure no error occurs. Wait for the Gradle Sync to finish.

Firebase dependencies is added to your project. Version code(10.0.1 for now) may vary while you
will be adding dependencies to your project.

Edit Mainfest

Now we will add a service ‘AppFCMService‘ in AndroidManifest.xml. This extends FirebaseMessagingService . We need this to receive downstream messages, send upstream messages, recieve payload from server or Firebase console, show push notifications when the app is in foreground/background. Copy the following snippet before closing <application> tag of your AndroidManifest.xml file.

Another service we need to create, for say ‘AppFirebaseInstanceIdService‘ which extends FirebaseInstanceIdService. This is required create/update registration tokens. Registration tokens are used to send messages to specific device or group of devices. Copy the following code like you did earlier in AndroidManifest.xml


Also you can set default icon,color of the notification by adding the following code snippet in your manifest.

Generate FCM Token

Next we will implement the service AppFirebaseInstanceIDService which we declare earlier in AndroidManifest.xml . Just create a new class(Make sure the class should be created within the root directory of your app) and copy the following code.

In the above code we are getting the FCM token automatically(whenever token is refreshed) in onTokenRefresh() method. After that we are sending it to our app’s server. So that we can store and send downstream message from server using that token.

Receive Message and Show Notification

Here we come with the most awaited functionality i.e. receiving and displaying push notification. We will implement the service AppFCMService mentioned earlier to achieve our goal. Here we go –

This service consists of two methods

  • 1. onMessageReceived() : Whenever message is received this method is invoked.
  • 2. showNotification() : Display the notification.
FCM : Notification Screenshot

In onMessageReceived() method we are receiving the data(RemoteMessage) from the server or Firebase console. Then we pass it to showNotification() method to build and show the notification(as shown in the picture).
The data can be sent from both Firebase console or your custom server. To send from your custom server you need to implement FCM in your server(built in PHP or Node.js etc) Read this article.

So, now we are familiar with Android Push Notifications using FCM. Hope you liked it. Feel free to comment or ask question if there is any query. Share it to inspire us.

Reference : Google Firebase

Abhishek is a developer cum blogger working more than 6 years. He loves programming especially open stack technologies. He has decent knowledge in Android development, Wordpress, MongoDB, Node.js and so on. Beside this Abhishek finds himself busy in painting, front-end designing.

Follow Abhishek


FCM Node Downstream Messaging
FCM downstream messaging using Node.js

Available for Amazon Prime