Firebase – Cloud Messaging


  • Firebase Cloud Messaging, (formerly known as Google Cloud Messaging), is a new and improved way of delivering Push Notifications to major platforms that includes Android, iOS and web.

Why to switch over it?

Of Course, one should have reason to switch from GCM to FCM. Below are those:

  • You can send unlimited Upstream and Downstream Notifications

  • You can send messages to individual devices or a user segment

  • You can queue notifications

  • You can schedule notifications

  • And above all, it has improved battery performance tunings

Getting Started


  • Android Studio 1.5 or above

  • Google Play Services 9.0 or above

Add Firebase to your app

  1. Go to Firebase console and click on “CREATE NEW PROJECT”. Fill the “Project Name” and Select Country, then click on “CREATE PROJECT”


  1. Click on “Add Firebase to your Android app”



  1. Enter Package name and Debug signing certificate SHA-1(Optional) and click on “Add app” button


  1. Google-services.json file will be downloaded. Go to Android Studio and Switch to the Project view in Android Studio to see your project root directory. Move the google-services.json file you just downloaded into your Android app module root directory.


  1. The Google services plugin for Gradle loads the google-services.json file you just downloaded. Modify your build.gradle files to use the plugin.

  2. Add the following lines, to your Project-level build.gradle (<project>/build.gradle):

[sourcecode language=”java”]
buildscript {
dependencies {
// Add this line
classpath ‘com.google.gms:google-services:3.0.0’

  1. Add the following lines to the bottom of the file, to your App-level build.gradle (<project>/<app-module>/build.gradle):

[sourcecode language=”java”]
// Add to the bottom of the file
apply plugin: ‘com.google.gms.google-services’

  1. Add the following dependencies, to your App-level build.gradle

[sourcecode language=”java”]
compile ‘com.google.firebase:firebase-messaging:9.0.2’

  1. Finally, press “Sync now”



  1. Register these services in your AndroidManifest.xml:

[sourcecode language=”xml”]
<service android:name=".MyFirebaseMessagingService">
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
<service android:name=".MyFirebaseInstanceIDService">
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>


2. Create two files as below:


[sourcecode language=”java”]
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = “MyFirebaseIIDService”;

public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, “Refreshed token:”+refreshedToken);

private void sendRegistrationToServer(String token) {
// save the token


[sourcecode language=”java”]
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = “MyFirebaseMsgService”;

public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, “From:”+remoteMessage.getFrom());
Log.d(TAG, “Notification Message Body:”+remoteMessage.getNotification().getBody());

private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(“FCM Message”)
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());

3. By default, you have to call nothing. FCM calls its services by itself. Still if you want, you can call:

[sourcecode language=”java”]

Note: This method should be called in separate thread. AsyncTask is preferable.


That’s it…!!! By now, you are all ready to get notifications. Don’t forget to add internet permission in your AndroidManifest.xml:

[sourcecode language=”xml”]
<uses-permission android:name=”android.permission.INTERNET” />


  1. Go to Firebase Console. Go to your app and on left side menu, go to Notifications. If visiting this for the first time, click on “Send Your First Message”


  1. Fill the message details and click on “Send Message”


You should receive a notification in your device.

Important Points to remember

  • FCM token is generated even without invoking its services. So, it would be helpful if you write a method inside onTokenRefresh() that will save the token to SharedPreferences to use it later whenever you want.

  • To generate token, you can call: FirebaseInstanceId.getInstance().getToken();

  • To delete token, you can call: FirebaseInstanceId.getInstance().deleteInstanceId();

  • Note that, generating a token and deleting a token operations are blocking Main Thread (or UI Thread), so it’s preferable to encapsulate this code in AsyncTask



You see, integrating push notifications has become insanely easy. Hope you enjoyed learning FireBase Push Notifications.

Want to work with us? We're hiring!