Sending a Notification Message using Firebase Cloud Messaging
In this section, we will talk about how we can send a test notification message from the Notification composer to a development device when the app is in the background of the device. For this, we have to:
1) Create a Firebase project.
2) Connect our app to Firebase either from the assistant or console and download the google-services.json file to our app directory.
3) In our project level build.gradle file, make sure to include Google’s Maven repository in both our buildscript and all project section.
4) For the app build.gradle file, we have two implementation library”
- Add the Cloud Messaging Android library and firebase core library to our app/build.gradle file:
- Implementation of ‘com.google.firebase:firebase-core:17.0.0’
- Implementation of ‘com.google.firebase:firebase-messaging:19.0.1’
- Also, add google play dependencies(classpath and apply):
- apply plugin:’com.google.gms.google-services’
- classpath ‘com.google.gms:google-services:4.2.0’
5) Edit our App Manifest
Notification channels are recommended and supported. FCM gives a default notification channel with basic settings. If we want to create and use our default channel, then we have to set default_notification_channel_id to the ID of our notification channel object. FCM will use this value when incoming messages do not explicitly set a notification channel:
6) Now, we will move to our main activity to make sure that the notification channels are created.
7) Access the Device Registration Token
On the initial startup of our app, the FCM SDK generates a registration token for the client app instance. If we want to target a single device, we will need to access this token by extending FirebaseMessagingService and overriding onNewToken. The token can be rotated after the initial startup, so it is strongly recommended to retrieve the latest updated registration token.
The registration token may change:
- When the instance ID is deleted by the app.
- When the app is restored on a new device.
- When the user reinstalls/uninstalls the app.
- When the user clears app data.
- Retrieve the Current Registration Token
When we need to retrieve the current token, call FirebaseIntanceId.getInstance().getInstanceId().4
9) Monitor Token Generation
The oneNewToken callback is fired when a new token is generated. When we obtained the token, we can send it to our app server and store it using our preferred method:
activity_main.xml
Main_activity.kt
MyFirebaseMessagingService.kt
When we click on the Log Token button, it provides us an InstanceID Token, which we use in our FCM console to send a message.
Now, we will move to our FCM console and click on New Notification.
After clicking on Nee Notification, it will ask us to fill out some of the fields such as notification title, text, and image, etc., and click on the Select test message.
We will paste the copied InstanceId Token to Add an FCM registration token field and click on the Test.
The last step creates a notification message in the notification bar of our app. The notification is shown only when our app will run on the background.