Firebase Cloud Messaging for Android: Sending Push Notifications

In this Firebase Cloud Messaging tutorial, you will learn how to add push notifications to Drink-It, an app that reminds you to drink water through the day. By Evana Margain Puig.

4.8 (21) · 1 Review

Download materials
Save for later

Firebase Cloud Messaging (FCM) is a set of tools that sends push notifications and small messages of up to 4 KB to different platforms: Android, iOS and web.

This topic is useful because you use push notifications in a lot of mobile projects. Firebase is one of the simplest methods to get notifications working.

In this project, you’ll build Drink-it, an app that sends push notifications throughout the day reminding you to stay hydrated. After all, with our busy lives, many of us forget how important water is.

In the process you’ll learn:

  • How to set up Firebase and create a project in it.
  • A method to connect your mobile app with the Firebase console.
  • The difference between receiving messages in the foreground and background of your app.
  • How to use the data received in the messages.
  • How to send test messages through Firebase console.
Note: This Firebase Cloud Messaging Tutorial assumes you know the basics of Android development, Kotlin and Android Studio. Also, it assumes that you already have a Google Account to configure the Firebase console. If you’re new to Android development, check out our Kotlin for Android Introduction or our Beginning Android Development With Kotlin series.

Getting Started

Download the starter project by clicking on the Download Materials button at the top or bottom of the tutorial. Open the starter project in Android Studio 3.6 or later.

Looking at the code, you’ll notice the starter project provides the user interface and some classes where you’ll add the logic of the app.

Build and run the starter project. You’ll see the Drink-It app with a button for retrieving a token. Right now, the button does nothing, but you’ll configure it soon.

Drink-It app initial view with a button to retrieve the token

What Is Firebase Cloud Messaging?

Push notifications — those small alerts that slide in from the top of our screen, letting us know an app needs our attention — have been around since the early days of Android apps. There are many tools that can help us add this functionality, but Firebase Cloud Messaging is one of the easiest and most straightforward to add in your projects.

Notification example

Firebase Cloud Messaging has a simple architecture with four main parts:

  1. A service, API or console that sends messages to targeted devices.
  2. The Firebase Cloud Messaging back end, where all the processing happens.
  3. A transport layer that’s specific to each platform. In Android’s case, this is called the Android Transport Layer.
  4. The SDK on the device where you’ll receive the messages. In this case, called the Android Firebase Cloud Messaging SDK.

You’ll use each of these parts throughout the tutorial. First, you’ll look at the setting up Drink-It in the Firebase console.

Configuring the Project in the Console

Before receiving any push notifications in your app, there’s a series of steps you’ll need to configure in the Firebase console. That’s what you’ll do in the first part of the tutorial.

Creating a Firebase Project in the Console

To begin, sign in to the Firebase console. Make sure you use the Google account you want to tie to your project.

After logging in, you’ll see one of two options:

Firebase console with create a project button if this is your first project

Firebase console with add project button if you have other projects

  1. If this is your first project, you’ll see a button with the label Create a project.
  2. If you’ve already started other projects, you’ll see a list of them and a button with the label Add project.

Once you get to the next screen, name the project and accept the terms of service. You can choose whatever name you want since you only need it to identify the project.

For this tutorial, you’ll set the name to Drink-It. Once the name is set, accept the Firebase terms and click continue.

Firebase console requesting information for the project name and service agreement

The next screen will prompt you to add Google Analytics. Click the checkbox to get statistics about your notifications, like how many people received them, if they opened them and more.

Analytics aren’t required for this tutorial, but you can activate them if you’re interested.

Screen prompting you to add Google Analytics to the project, which is optional

Finally, click Create project and wait until it says it’s ready. You’ll see an image like the one shown below.

Project creation was successful

Congratulations, you just created a Firebase project that works for Android, iOS and web!

Registering the App With Firebase

Now, you’ll continue to configure your Android app. Once the Firebase project is ready. Click Continue to display the main screen of the project.

Once on the project home page, you’ll notice several options and details. Here’s where you’ll find the option to add Firebase to an app.

Project hompage with options to add Firebase to an app

To start integrating Firebase with your project, click the Android button, which is under Get started by adding Firebase to your app. You’ll see a screen requesting data from your app.

Firebase screen that requests information from your Android app

Enter the following information:

  • Android Package Name: This is important. It must match your application ID.
  • App Nickname: Drink-it.
  • Debug signing certificate SHA-1: You don’t need this, just leave it empty.

Screen with the Android app details filled in

Next, click Register app to proceed to the next step.

Adding Firebase Configuration Files to Your Android Project

This screen prompts you to download a JSON file that contains the service configuration for Firebase. Save it anywhere on your computer and remember where you place it. You’ll add it to the project next.

Screen to download the JSON file

Go into Android Studio and switch to the project view in the left panel:

Project View in Android Studio

Add the file to the project in the path DrinkIt/app:

Project tree with the JSON file added

Note: Your starter project already contains a file named google-services.json, but you still need to replace it with the one you just downloaded. The starter project includes this file so Android Studio can compile and run the starter project at this initial stage. When you start your own app from scratch, you won’t have this file in the project folder.

Adding the Firebase SDK to the App

In Android Studio, switch back to the Android view in the left panel:

Android view in Android studio left pane

Open build.gradle, which Android Studio tags as (Project: DrinkIt). This file is in Gradle Scripts/build.gradle.

Next, add this code in the dependencies section:

classpath ''

Click Sync Now in the yellow warning at the top and wait for the Gradle sync.

Android studio yellow bar at the top requesting to sync gradle

The project build should be successful.

Android Studio build window indicating build was successful

Then open Build.gradle, labeled as (Module: app). This file is also in the Gradle Scripts section within the Android view.

Next, add this code at the top with the other, similar lines:

apply plugin: ''

If you chose the Google Analytics option during the Firebase project creation, add this in the dependencies section:

implementation ''

Once again, click Sync Now and wait for the build to finish.

Go back to the Firebase console and click Next in the screen where you downloaded the file. You’ll see a small prompt that checks whether your app has connected to the server or not:

Firebase console indicator checking if the app is communicating with the server

Build and run to make this check successful. The app will look like the image below with no changes in the UI:

Successful app build

If the check was successful, the yellow warning will change to a congratulations message:

Message showing your app has communicated correctly with Firebase

If everything is correct, click Continue to console. Otherwise, go back over the steps above and check you did everything correctly.

Adding Firebase to an app with a confirmation button

You’ll now see the console homepage and the Android logo:

Name of the project with an Android purple logo indicating the app has Firebase integrated now

Build and run. You shouldn’t see any changes in the app.

Drink-it app with no changes