You can easily send Android push notifications using Firebase Cloud Messaging. In today’s article, we will discuss the steps and tricks to implement the process.
But first, let's cover the basics of implementing push notifications in Android devices.
What are push notifications?
A push notification is typically an opt-in alert that displays text or other media like images, emojis, or GIFs that enable users to take a specific action.
The notifications serve as a communication channel to enable users to convey messages, offers, and any updates to their audience.
An example push notification is a Twitter post update for a user you already followed to get notified whenever they post.
The button for getting these Twitter user notifications is on the profile section, often a bell icon.
The anatomy of push notifications
The typical design for a push notification consists of the following elements:
- Title
- Message
- Image ( GIF, Emoji)
- URL
However, the first-ever opt-in message you send will build or break your push notification efforts.
For this reason, it's essential to communicate your value proposition and call them to action. Ask them if they want to receive your notifications first.
Types of push notifications
We have several types of push notifications which include;
1. Web push notifications
The notifications are sent to a user through the desktop web or mobile web. The message slides from the top or bottom right-hand side of the desktop.
These notifications are majorly used by digital marketers to boost their conversions. App developers who also have tools like Chrome extensions can use these notifications to send app updates and more to subscribers.
2. Mobile app push notifications
If your users already have the application downloaded on their phones, push notifications are triggered on their mobile devices.
When the users open the app, unique identifier registers for both the application and the operating system of the mobile device. Push notifications on the phone appear in three areas:
- Lock screen
- Banner
- Notification center
What is Firebase Cloud Messaging used for?
Initially known as Google Cloud Messaging (GCM) for Android, Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send message notifications at no cost.
Firebase Cloud Messaging solution is used beyond sending push notifications. Other features that developers access with FCM include:
- Ad mob
- Analytics
- In-app messages
- Remote configuration
- Social logins
- Cloud functions
FCM allows Android developers and other app developers to send push notifications to end users through an application programming interface (API).
What are the two types of messages in Firebase Cloud Messaging (FCM)?
There are two types of messages sent using Firebase Cloud Messaging (FCM):
1. Data messages
Data messages are information generated, sent, received, stored, and processed by electronic means.
Software developers cannot send data messages through Firebase Console unless using Firebase API. However, it must- have server-side logic. FCM places a 4KB limit on data messages. If you exceed the limit, you will need additional data using tools like JobScheduler and WorkManager API.
2. Notification messages
The notification messages include icons, titles, messages, app names, and more.
These push notifications are ways to let subscribers get notified on offers, updates, and more directly on desktop and mobile devices. Here is a visual of notifying users with Firebase Cloud Messaging (FCM).
Why send push notifications using Firebase Cloud Messaging?
There are several reasons why you should send push notifications using Firebase Cloud Messaging:
1. Send messages to any device
Firebase Cloud Messaging (FCM) enables developers to send messages beyond Android devices. Other devices include; iOS and Web at no cost.
It provides a reliable and battery-efficient connection between the server and devices.
2. Send personalized notifications content
Send custom data and messages now or in the future to the user’s local time zone. Also, deliver notifications messages on the go and track conversion events.
3. A/B test notifications
You can A/B test different versions of your notifications messages. In partnership with Google Optimize, FCM integration empower you to test your messages on the web.
Segmentation that goes beyond demographics. FCM advanced messaging tools enable you to target subscribers with custom messages.
Additionally, notification messages are fully integrated with Google Analytics for Firebase. You can monitor effectiveness from a single dashboard with no coding required.
How to send Android push notifications using Firebase Cloud Messaging (FCM)
An FCM implementation includes two components for sending and receiving message notifications;
- A reliable environment such as Cloud Functions for Firebase on which to build, target, and send messages.
- An Android, iOS, or web (Javascript) that receives messages via the corresponding platform-specific transport service.
Understanding Firebase Cloud Messaging (FCM) Architecture
The FCM architecture that builds, transport, receives, and sends push notifications has the following components;
The right tools to compose and build messages. To build full automation for all message types, you must build on trusted server environments.
Furthermore, they have to support Firebase Admin SDK or the FCM server protocols. The environment can be your own app server, Cloud Functions for Firebase, or App Engine.
- The FCM back-end accepts message requests, performs a fanout of messages via topics, and generates message metadata like the message ID.
- A transport layer, in this case, the Android transport layer (ATL) for Android devices to route the message to the target audience. Web push protocol for web apps.
- The FCM SDK on the user’s device where the push notifications are displayed.
Step-by-step process to send push notifications on Android using FCM
Using Firebase Console, you download Firebase config files from the console and then move them into your Android project.
Step 1: Create a Firebase project to connect to your Android App. Learn how Firebase projects work.
Step 2: Register your Android app with the Firebase project.
Step 3: Add a Firebase configuration file
- Download and then add the Firebase configuration file (google-services.json)
- Do make the values in your google-services.json config file accessible to Firebase SDKs, you need the Google services Gradle plugin (google-services).
Add the Google Services plugin as a buildscript dependency;
buildscript {
repositories {
// Make sure that you have the following two repositories
google() // Google's Maven repository
mavenCentral() // Maven Central repository
}
dependencies {
...
// Add the dependency for the Google services Gradle plugin
classpath 'com.google.gms:google-services:4.3.15'
}
}
allprojects {
...
repositories {
// Make sure that you have the following two repositories
google() // Google's Maven repository
mavenCentral() // Maven Central repository
}
}
b. In your module (app-level) Gradle file (usually <project>/<app-module>/build.gradle
), add the Google services plugin:
plugins {
id 'com.android.application'
// Add the Google services Gradle plugin
id 'com.google.gms.google-services'
...
}
Step 4: Add Firebase SDKs to your app
In your module (app-level) Gradle file (usually <project>/<app-module>/build.gradle
), add the dependencies for the Firebase products that you want to use in your app. We recommend using the Firebase Android BoM to control library versioning.
dependencies {
// ...
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:31.4.0')
// When using the BoM, you don't specify versions in Firebase library dependencies
// Add the dependency for the Firebase SDK for Google Analytics
implementation 'com.google.firebase:firebase-analytics'
// TODO: Add the dependencies for any other Firebase products you want to use
// See <https://firebase.google.com/docs/android/setup#available-libraries>
// For example, add the dependencies for Firebase Authentication and Cloud Firestore
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-firestore'
}
After adding the dependencies for the products you want to use, sync your Android project with Gradle files.
Conclusion
Finally, when testing pushing notifications on Andriod using FCM, you simplify and send test notification messages from the Notification composer.
To custom push notifications with Firebase In-app Messaging, you either use the Firebase default displays or create your own message library from scratch.
Additionally, you can send messages to multiple devices in two ways:
- Topic messaging that focuses on the particular topic a subscriber wants to receive.
- Device group messaging allows you to send the same message to multiple devices ( desktop, web, mobile) to a group you define.