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.

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

build.gradle(root)

1

2

3

4

5

6

7

8

9

10

11

12

buildscript{

....

....

dependencies{

....

classpath'com.google.gms:google-services:3.0.0'

}

}

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

build.gradle(app)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

apply plugin:'com.android.application'

android{

...

}

dependencies{

...

compile'com.google.firebase:firebase-core:10.0.1'

compile'com.google.firebase:firebase-messaging:10.0.1'

}

apply plugin:'com.google.gms.google-services'

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.

AndroidManifest.xml

1

2

3

4

5

6

7

8

9

10

11

<service

android:name=".AppFCMService"

android:exported="false">

<intent-filter>

<action android:name="com.google.firebase.MESSAGING_EVENT"/>

</intent-filter>

</service>

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

AndroidManifest.xml

1

2

3

4

5

6

7

8

9

10

11

<service

android:name=".AppFirebaseInstanceIDService"

android:exported="false">

<intent-filter>

<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>

</intent-filter>

</service>

OPTIONAL

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.

// TODO: Implement this method to send any registration to app's server.

}

}

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 –

1. onMessageReceived() : Whenever message is received this method is invoked.

2. showNotification() : Display the notification.

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.

Abhishek is a developer cum blogger working more than 4 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

Next

Featured Post

ExoPlayer is an Android Application level Media Player by Google. It is used for playing media(both audio and video) from local or remote. In this article we will discuss how to play or stream remo ... Read More