Firebase Push Notifications – Send and Receive Notifications with FCM
Learn how to send push notifications using Firebase Cloud Messaging (FCM) in your Android app. This tutorial covers setting up FCM, sending notifications, and receiving them in your app.
Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that allows you to send notifications and messages to users on Android, iOS, and the web. FCM helps you engage users with notifications and notifications-triggered actions (such as opening the app, navigating to a specific screen, etc.).
i) Setting Up Firebase Cloud Messaging (FCM) in Your Android App
To get started with FCM, you need to set up Firebase and integrate FCM into your Android project.
Step 1: Add Firebase to Your Android Project
- Go to the Firebase Console: Firebase Console.
- Create a new project or select an existing one.
- Add your Android app to the Firebase project and download the
google-services.jsonfile. - Place the
google-services.jsonfile in theapp/directory of your Android project. - In your app-level
build.gradlefile, add the following dependencies:
Then sync the project with Gradle.
Step 2: Enable Firebase Cloud Messaging
- In the Firebase Console, go to the Cloud Messaging section.
- Firebase will automatically generate a Sender ID and Server Key that you’ll use to send notifications.
ii) Handling FCM Notifications in Android
Now that Firebase Cloud Messaging is integrated, let’s set up background and foreground notification handling.
Step 1: Create a Service to Handle FCM Messages
Firebase allows you to send both notification messages (displayed automatically by FCM) and data messages (handled manually by your app).
- Create a custom
FirebaseMessagingServiceto handle incoming messages.
- Register this service in your AndroidManifest.xml:
This service will receive both data and notification messages. The onMessageReceived() method will be called when a message is received, either in the foreground or background.
iii) Sending Push Notifications from Firebase Console
Firebase Cloud Messaging allows you to send notifications directly from the Firebase Console.
- Go to the Firebase Console.
- Navigate to Cloud Messaging under the Firebase Cloud Messaging section.
- Click on Send your first message.
- Enter a title and message body for the notification.
- You can send the message to specific devices, topics, or user segments. For testing, you can send it to your app instance by using the device’s token.
- Click Send Message.
This sends a simple notification to your device, and the message will be displayed in the notification drawer (if the app is in the background).
iv) Sending Push Notifications Programmatically
You can also send notifications programmatically from your server using Firebase's REST API. Here’s an example of how to send a data message (without a notification) to a device.
Step 1: Get the Device Token
The device token can be retrieved when your app registers with Firebase:
Step 2: Send Notification Using FCM Server API
To send a notification via Firebase Cloud Messaging API, you can use a POST request to the Firebase Cloud Messaging endpoint with the device token:
- Endpoint:
https://fcm.googleapis.com/fcm/send - Headers:
Authorization: key=YOUR_SERVER_KEYContent-Type: application/json- Body (Example for data message):
In Java/Kotlin (using Retrofit or OkHttp):
This will send a data message to the device with the given token.
v) Handling Notifications in the App
When the app is in the foreground, the notification message will not appear automatically. Instead, you can handle it and show it using the NotificationManager.
Foreground Notification Handling
In the onMessageReceived() method of FirebaseMessagingService, you can create a notification:
This method will create and display a notification when the app is in the foreground.
vi) Firebase Push Notifications – Benefits
- Cross-Platform Support: FCM supports Android, iOS, and the Web, enabling you to send notifications across all platforms.
- Real-time Delivery: Push notifications are delivered in real-time, providing an engaging experience for users.
- Automatic Background Handling: Notifications are automatically handled when the app is in the background or closed, ensuring seamless integration.
- Custom Data Messages: You can send custom data along with notifications to trigger specific actions in your app.
- Segmentation: Firebase allows you to target specific devices or groups of users based on topics, interests, and behavior.
vii) Conclusion
In this tutorial, we have learned how to integrate Firebase Push Notifications (FCM) into an Android app. We’ve covered the steps to set up Firebase Cloud Messaging, handle notifications when the app is in the foreground and background, and send notifications programmatically from your server.
By using FCM, you can easily send real-time notifications to your app users, engage them, and trigger specific actions based on the data you send with the notification.