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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Firebase Cloud Messaging for Android: Sending Push Notifications
30 mins
- Getting Started
- What Is Firebase Cloud Messaging?
- Configuring the Project in the Console
- Creating a Firebase Project in the Console
- Registering the App With Firebase
- Adding Firebase Configuration Files to Your Android Project
- Adding the Firebase SDK to the App
- Configuring the Demo App
- Including the Maven Repository
- Using the Device Token
- Retrieving the Device Token
- Checking if Devices Can Receive Notifications
- Sending a Test Message With Notification Composer
- Configuring Notification Composer
- Background-Restricted Apps
- Setting Custom Icons and Colors
- Receiving Notifications in the Foreground Versus Background
- Displaying Notification Data in Activities
- Sending a Payload in the Notification
- Where to Go From Here?
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.
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.
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.
Firebase Cloud Messaging has a simple architecture with four main parts:
- A service, API or console that sends messages to targeted devices.
- The Firebase Cloud Messaging back end, where all the processing happens.
- A transport layer that’s specific to each platform. In Android’s case, this is called the Android Transport Layer.
- 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:
- If this is your first project, you’ll see a button with the label Create a project.
- 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.
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.
Finally, click Create project and wait until it says it’s ready. You’ll see an image like the one shown below.
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.
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.
Enter the following information:
- Android Package Name: com.raywenderlich.android.drinkit. 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.
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.
Go into Android Studio and switch to the project view in the left panel:
Add the file to the project in the path DrinkIt/app:
Adding the Firebase SDK to the App
In Android Studio, switch back to the Android view in the left panel:
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 'com.google.gms:google-services:4.3.3'
Click Sync Now in the yellow warning at the top and wait for the Gradle sync.
The project build should be 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: 'com.google.gms.google-services'
If you chose the Google Analytics option during the Firebase project creation, add this in the dependencies section:
implementation 'com.google.firebase:firebase-analytics:17.2.3'
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:
Build and run to make this check successful. The app will look like the image below with no changes in the UI:
If the check was successful, the yellow warning will change to a congratulations message:
If everything is correct, click Continue to console. Otherwise, go back over the steps above and check you did everything correctly.
You’ll now see the console homepage and the Android logo:
Build and run. You shouldn’t see any changes in the app.