Javatpoint Logo
Javatpoint Logo

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.

Sending a Notification Message using Firebase Cloud Messaging

4) For the app build.gradle file, we have two implementation library"

  1. Add the Cloud Messaging Android library and firebase core library to our app/build.gradle file:
    1. Implementation of 'com.google.firebase:firebase-core:17.0.0'
    2. Implementation of 'com.google.firebase:firebase-messaging:19.0.1'
  2. Also, add google play dependencies(classpath and apply):
    1. apply plugin:'com.google.gms.google-services'
    2. classpath 'com.google.gms:google-services:4.2.0'
Sending a Notification Message using Firebase Cloud Messaging

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:

  1. When the instance ID is deleted by the app.
  2. When the app is restored on a new device.
  3. When the user reinstalls/uninstalls the app.
  4. When the user clears app data.
  5. 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

Sending a Notification Message using Firebase Cloud Messaging

Main_activity.kt

MyFirebaseMessagingService.kt

Sending a Notification Message using Firebase Cloud Messaging Sending a Notification Message using Firebase Cloud Messaging

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.

Sending a Notification Message using Firebase Cloud Messaging

Now, we will move to our FCM console and click on New Notification.

Sending a Notification Message using Firebase Cloud Messaging

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.

Sending a Notification Message using Firebase Cloud Messaging

We will paste the copied InstanceId Token to Add an FCM registration token field and click on the Test.

Sending a Notification Message using Firebase Cloud Messaging

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.

Sending a Notification Message using Firebase Cloud Messaging






Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA