How to Integrate FCM in Android App (Tutorial)



When Google (now Alphabet) acquired Firebase, it was evident that we’ll be seeing a lot more of it soon. Today, Firebase has officially replaced GCM (Google Cloud Messaging), and added a lot of products for Developers.

“Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.” – Google Developers Official Website

Okay, I get it. But why should I bother to upgrade to FCM? I’m used to GCM, and frankly I don’t have any issues with it! It’s just the next version of GCM, right?

Not exactly! Let’s dig a little deeper.

What’s New in FCM as Compared to GCM?


In short, FCM has everything GCM had – with a few perks! Similar to GCM, it also supports messaging for Android, iOS, and Chrome. The part where FCM improves is – development effort. Frankly, registering and handling retry logic in GCM was a little tedious. FCM has improved on this, and made client implementation a breeze. You’ll see that for yourself in a moment.

Apart from that, Firebase also offers a console to send notifications to target audience. Yes! You can now send notifications right from Firebase Console without any server-side implementation on your end. If your project currently runs on GCM, don’t panic! GCM is still supported. The only catch is, any new features will not be added in GCM. It’ll stay as-it-is today, for a long time – and eventually it’ll get deprecated.

So, are you geared up to take this step?

integrate fcm android app native

image source: Google Developers Blog

Minimum Requirements for Firebase


Before beginning with this tutorial, you should have the following prerequisites…

  1. An Android project.
  2. Android Studio v1.5 or higher
  3. Google Play services SDK installed

Do note that Firebase is only supported on devices running Android v2.3 (Gingerbread) or later, and Google Play services 9.2.0 or later.

Add Firebase SDK to your Android App


1. Open Firebase Console and click Create New Project. You may want to import an existing Google project, if any.

Firebase Console

2. Give your project a name, select your country, and click Create Project. The project name could be anything. It’s just for your reference.

Firebase Project Details

3. You’ll now see 3 options to add Firebase to your Android app, iOS app, or Web app. For this tutorial we’ll stick to Android app only. Integrating it to iOS or web app is fairly simple as well. Click on Add Firebase to your Android App to proceed.

Add Firebase to Android App

4. It’ll then ask you to enter package name of your app, along with SHA-1 fingerprint. For Cloud Messaging/Push Notifications you may ignore SHA-1 certificate. Just type in the package name of your application and click Add App.

Package Name

6. Once that is done, it’ll generate a google-services.json config file. Save this file to <your-android-project-directory>/app/google-services.json

Tip: If you’re using different product flavors with distinct package name (application ID) for your Android project, you should create separate projects for each flavor in Firebase Console. Also, each app’s google-services.json should be saved to <your-android-project-directory>/app/src/<flavor-name>/google-services.json

google-services.json Firebase SDK

7. Time to add Firebase SDK to your Android project.

Add Google Services dependency to project-level build.gradle file:

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

And this to module-level build.config:

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

That’s it! Click Sync Now and Firebase SDK is successfully integrated with your project.

Add Firebase SDK dependencies

Integrate FCM (Firebase Cloud Messaging) Push Notifications in Android App


Now that you have Firebase SDK integrated with your Android app, there’s just one more thing needed to start sending upstream and downstream push messages through Firebase Console – add Firebase Messaging library.

1. Add FCM library dependency to module-level build.gradle, and sync project.

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

2. Create a new class by extending FirebaseInstanceIdService class. This class will be used to get FCM token for sending and receiving push messages.

3. Create a new class extending FirebaseMessagingService to push messages.

4. Add both classes’ reference in your module’s AndroidManifest.xml

5. That’s all! Downstream push messages should work through Firebase Console now.

Next Steps


Basic setup is done. Yay! Now, you should work on the server end based on your project requirements. You may refer to the following resources for server side implementation:

  1. Send push notifications to specific devices
  2. Notifications to devices subscribed to a topic
  3. Downstream messages to a group of devices
  4. Upstream messages from device

Bonus: Guess what? Firebase includes analytics by default – and you just added it to your app as well – without even knowing.

Did you encounter any issues while integrating Firebase Cloud Messaging in your project? Do share your suggestions in comments section below!