Integrate Firebase Cloud Messaging FCM on Android

In the early time, Google was announced C2DM service for sending message downstream to client application. C2DM has many weakness so GCM Google Cloud Messaging come out for the replacement to improve the service. Now, the latest Firebase Cloud Messaging has inherits all the features of Google Cloud Messaging and adding some new features on it. It is a new messaging service that suggest to migrate from GCM or C2DM. In this tutorial, I will tell you how to integrate Firebase cloud messaging fcm on android to send downstream push notification to client side application.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “FirebasePushNotificationExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add project in Firebase Console

Go to Firebase console https://console.firebase.google.com/  and create a new project. Enter your project name and select which country you live.

firebase console add project

Add firebase to Android App

Inside the project control panel select add firebase to android app. Enter your android project and copy the package name into the firebase console package name section. After that click Add app button.

add pacakge name

Move google-services.json into your project

It will automaticaly download a json file into your computer. Then switch to the Project view in Android Studio to see your project root directory and move the it into your Android app module root directory.

move json file in your project

Edit build.gradle

In the project-level build.gradle add the following line.

After that go to your app-level build.gradle, apply the plugin and add a new dependency

This show insert below the dependency if not it will occur error.

Now, you can synchronize your android project.

Edit AndroidManifest.xml

Add two firebase services in your application so that you can get Token and notification from firebase cloud server.

Create a two new class for FirebaseInstanceIDService and FirebaseMessagingService

Right click your package name > New > Java class. Name it to MyFirebaseInstanceIDService. After that, create another java class name it to MyFirebaseMessagingService. Both services name should be same as the name define in the androidmanifest.xml.

Edit MyFirebaseInstanceIDService.java class

Go to the MyFirebaseInstanceIDService that just create, edit it to the following source code. This file is to get the Token from current user that register to the firebase server.

Edit MyFirebaseMessagingService.java class

Then go to the MyFirebaseMessagingService and edit the class. This file is to get the message from the firebase server and the message will put it to the notification.

Edit activity_main.xml layout

This layout display two button which are subsribe button and get token button.

Edit MainActivity.java class

In this class, add button listener to both button to perform specific functionality. You can try it later.

Run your project

Now, you can try and run it in your android device to check out the firebase cloud messaging.

Send Push Notification

Go to Firebase Console and send push notification now. Go to notification section from left hand side and enter message text and label. And select what target you wan to push.

after integrate firebase cloud messaging fcm and send push notification

Yea…. you are successfully to create a push notification.

(Integrate Firebase Cloud Messaging FCM on Android)

Source Code

(Visited 726 times, 1 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *