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"
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 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:
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.