Developer's guide to setup Android Push Notification using FCM (Firebase Cloud Messaging)
November 10, 2022
Table of Contents
What is Push notification?
Benefits of sending push notifications
What is FCM?
How does FCM push notification work?
Before digging deep into push notification integration, let’s start by answering some basic questions - “What is a push notification” and “What are the benefits of sending push notifications”
What is push notification?
Push notifications are pop-up messages sent to a mobile app. It appears in the notification tray or on the lock screen [if the device is locked] of your mobile device. These notifications are designed to grab user’s attention and increase the app interaction rate. Generally used to send reminders, updates, and other alert messages.
Benefits of sending push notifications
Every day we receive push notifications on our smartphones and web browsers. It has become a common mode of communication for applications and If implemented correctly, they can be extremely valuable to drive user engagement.
Along with being a no-cost communication channel, push notification has a high click rate (~10% if targeted correctly) which is 5x higher than email notifications. According to a survey conducted by reviews.org, the average U.S. user checks his phone “344 times a day, i.e., every 4 minutes” and 70% of the users have a tendency to check their phones within 5 mins of receiving a notification. Though push notifications clearly seem to drive engagement, apps should restrain from sending too many notifications (1 per day or max 5 per week). Otherwise, users might unsubscribe or uninstall the app.
In this article, we’ll cover how you can setup Android Push notifications using google Firebase Cloud Messaging (FCM)
What is FCM?
Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost. Using FCM, you can notify a client app about product updates in real-time. For use cases such as instant messaging, a message can transfer a payload of up to 4 Kb to a client app at a time.
How does FCM push notification work?
Let’s first understand the actors involved in completing a push transaction -
Push Service (FCM Server) — Cloud service from Google that allows app developers to send notifications to users across a variety of platforms, including Android, iOS and web applications.
User Device — The devices/endpoint on which the user is using your mobile / web app and the push notification will be shown.
Your Server — This is the server that will decide what content needs to be sent to the Push Service in turn to send a push notification to the end user’s device.
Next step is to understand how the communication flows from FCM server to user's device:
Once the user installs the application on their device, the app requests the FCM Server to generate a unique FCM device token for this endpoint (user’s device)
This FCM device token is sent to Your Server in case you have to send a 1–1 push message towards this specific endpoint (user’s device).
Note - FCM also provides a solution to broadcast a particular message to a set of users and it is not mandatory to have users’ device token in such cases.
Now based on your business logic, you can trigger push notification to the user’s device by calling a simple POST HTTP FCM API.
Let’s start sending push notifications using Firebase
Irrespective of the tech stack you are using (flutter, native, react native etc.), below are the core steps to follow for setting up FCM push notifications
Create a Firebase project in the Firebase console (You can skip this step if you have an existing Firebase project)
Register your Android App with Firebase console
Add Firebase configurations to your app
Send Push Notifications from backend using cURL command (this is a just a sample code snippet, you can check this as per your backend stack)
Step 1[Optional] — Create a Firebase project in the Firebase Console
Firebase project is where your app will be registered. You can attach more than one app to the same project but not the same app to multiple projects. Your Firebase configuration keys will also be unique to the project.
To create FCM Project, Let’s first sign in to Firebase Console. Make sure you use the Google account you want to tie to your project. After logging in, you’ll see 2 options:
If this is your first project, you’ll see a button with the label Create a project. Click on “Create a Project”
If you’ve already started other projects, you’ll see a list of your existing projects and a button with the label Add project. Click on “Add Project”
Once you get to the next screen, give your project a name. You can add any name since it will be needed just to identify your project. For this blog, we’ll name it “AwesomeName”. After adding the name, accept terms of service and Click on “Continue”.
Next, you’ll see the option to enable Google Analytics for your project. This is an optional step. Enable it to track notification performance, like delivery rate, open rate, click rate and more. Click on “Continue” to proceed to the next step.
Choose your Google Account where you would want to configure your data. If you don’t have an existing google analytics account, select “Default Account For Firebase”. This will create a default firebase property in your google analytics account.
Finally, click on “Create project” and wait until it’s ready. You’ll see a similar image like the one shown below once your project is ready.
Congratulations, you just created a Firebase project that works for Android, iOS and web!
Click on “Continue” to move to the 2nd step
Step 2 — Register your Android App with Firebase console
On the project homepage, you’ll see an android app icon.
Click the “Android button” to start integrating your android app
This will open the register app page. Enter the following information:
Android package name - You can find this in your app level “build.gradle” file
App Nickname (optional) - Provide some nickname for this app. If specified, the app nickname will be used throughout the Firebase console to represent the app. This nickname isn’t visible to users.
Click on “Register App” to proceed to the next step
Step 3 — Add firebase configurations to your Android App
This screen prompts you to download a JSON file that contains the service configuration for Firebase. Download the JSON file google-services.json and put this in your app folder by following the below steps:
Open build.gradle at project level (project/build.gradle)
Add google() to buildscript and allprojects repositories
Add classpath ‘com.google.gms:google-services:4.3.10’ to dependencies
Open build.gradle at app level (project/appmodule/build.gradle)
Click on the “Sync Now” button and wait for the build to finish. This will establish a connection between your application and Firebase project
Now go back to Firebase console and click “Next” on the screen from where you have downloaded the file. You’ll see a small prompt that checks whether your app has connected to the server or not.
If the check is successful, you'll see a congratulations message like below. Click on “Continue to console” if you see the success message. Otherwise, recheck if you have followed the integration steps correctly.
Register your application with FirebaseMessagingService using this code sample. By registering this service, your application will be able to receive the FCM message payload.
Now your app is configured in the firebase console. Next we will trigger a sample push notification from backend using cURL request.
Step 4 — Sending push notifications from backend
Here’s a sample Curl Request to send push notification from your backend code. Do not forget to replace the key with your server key.
To get server key,
Open Firebase console
Go to Project Settings
Go to Cloud Messaging Tab
Below you will find the server key
and voila…
You just triggered your first notification using FCM. We have just triggered a simple text message but there are a lot of functionalities and styling options available in Android push notification. We’ll cover all the advanced configurations and the methods to set those configurations in our next blog.
However, you can skip all these steps and save the effort and time of integration. Just integrate SuprSend into your app and leave aside the technical aspect of handling
Integration of Android Service & Receiver
Advanced Notification configuration for sending all types of notification content
Handling Notification support for different Android API Level
Setting up notification tracking in your backend service
Handling notifications in background and Foreground mode and much more…
With SuprSend, you can start sending notifications to all the channels (android, ios, web push, sms, email, slack, whatsapp, app inbox ) with a single API integration. Also, with advanced workflows, you can handle all types of notification use cases such as crons, reminders, digest, multi-lingual messages and so on.
Now, you can focus your engineering bandwidth on solving your core business problem, and let SuprSend handle the notification infrastructure for you. Give SuprSend a try!
Share it on:
ABOUT THE AUTHOR
Eaque soluta nesciunt. Dolorem vel qui ut. Quis ut consequatur dolor magnam aliquam aut voluptatibus.Vitae doloremque omnis blanditiis impedit ea. Et et est enim dolor natus inventore velit voluptatem. Nihil id consectetur. Consequatur odio sapiente et accusantium et.