Firebase Cloud Messaging (FCM) Android Example and Tutorial


       In this Firebase Cloud Messaging tutorial, we will see how to send push notification to android device using Firebase Cloud Messaging. So let’s begin.

Why FCM over GCM?
        Previously Google has its messaging service named GCM (Google Cloud Messaging). But then there are some grey area reasons to switch to FCM.

1. Google is investing in Firebase, making it its unified mobile platform. Over the last few years, Google have expanded GCM to send messages to multiple platforms beyond Android: iOS and Chrome. Firebase is known for being cross platform, so FCM now makes a natural fit in the Firebase suite of features designed for Android, iOS, and mobile web.

2. With FCM we have Firebase console available to send push notifications on a click; in addition to FCM sever APIs that can be called from backend server to send push notifications.

3. The registration token lifecycle is very much simplified with FCM.

So now we have good reason to learn FCM, let’s start configuring FCM for your project. This is one-time process.

Step 1 : Create simple Android project in Android studio.
Step 2 : Get your package name from AndroidManifest.xml. Mention user permission for Internet access in AndroidManifest.xml file.

Step 3 : Go to firebase console and create a new project.

Step 4 : Window will appear as below. Give a project a name, select your country and click on “CREATE PROJECT” BUTTON

Step 5 : Now click on Add Firebase to your Android App.

Step 6 : Mention package name that you got from step 2.

Click on “ADD APP” button. google-services.json file will get downloaded; add to your project as shown below.

Click on “CONTINUE” button. Follow step as shown in figure and click FINISH.

Now we are ready with setup. Lets start with Implementing Firebase Cloud Messaging
Create a class “” extending “FirebaseMessagingService”


import android.content.Intent;


public class MyFirebaseMessagingService extends FirebaseMessagingService {
public void onMessageReceived(RemoteMessage remoteMessage) {
// creating notification

Intent i= new Intent(this, TargetActivity.class);
PendingIntent pi= PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder bn= new NotificationCompat.Builder( this)
.setContentTitle("CTE FCM Tutorial")
.setAutoCancel( true )
NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

In above code we have created PendingIntent for opening TargetActivity when user clicks on received notification.

Simplest can be as follow:


import android.os.Bundle;
import android.widget.TextView;

public class TargetActivity extends AppCompatActivity {

private TextView textView ;

protected void onCreate(Bundle savedInstanceState) {
textView = (TextView)findViewById(;
textView.setText("The Target Message Can Be Shown Here ");

Now we have to define the above service in our AndroidManifest.xml file. So go to manifest and modify as follows.

Complete AndroidManifest.xml is as below:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />

<activity android:name=".MainActivity">
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

<service android:name=".MyFirebaseMessagingService">
<action android:name="" />
<activity android:name=".TargetActivity">




That’s all. Your app is ready to receive notifications.

Sending Push Notification using Firebase Console

Go to firebase console and select the app you created.
From the left menu select notification.
Click on new message.

Set the message, select your application package from dropdown as shown in below.

Those who have installed your application will get the push notification.

Download Complete Project here

No comments:
Write comments

Popular Posts