SetUp Firebase Cloud Messaging in Android - Part 2 - Androhub

SetUp Firebase Cloud Messaging in Android – Part 2

In the previous article, we learned how to connect FCM to any Android application and get the token in the Android device.

In this article, we will learn how we can send push notification from Firebase Console and receive it in Android device.

VIDEO TUTORIAL

Types of Messages

With FCM, you can send two types of messages to clients:

1. Notification Message: These are handled by the FCM SDK automatically. Below is the JSON-formatted notification message which can be sent to the client.

2. Data Message: These are handled by the client app. Below is the JSON-formatted data message which can be sent to the client. Here you can give any key-value pair JSON data that the client can receive.

Notification Messages with Data Messages

We can send both notification and data payload together and below is the JSON-formatted notification-data message which can be sent to the client.

Note:

1. When the app is in the background or killed state then apps receive the notification payload in the notification tray and only handle the data payload when the user taps on the notification.
2. When the app is in the foreground, your app receives a message object with both payloads available.

App state Notification PayloadData PayloadBoth Payload
Foreground/RunningonMessageReceived()onMessageReceived()onMessageReceived()
Background/KilledNotification trayonMessageReceived()Notification: Notification tray
Data: In extras of the intent.

Sending Notification Payload

To send the Notification payload push notification follow the below steps:

1. Navigate to Firebase Console and select Cloud Messaging from the left side list.

Cloud Messaging

2. After that click on the Send your first message button to compose the notification.

Send First Message

3. Now compose the notification by entering the Notification title, Notification Body/text. It will show the preview on the right side device preview.

Compose Notification

4. Once you input the above details Sent test message button on the right side will get enabled and click on it.

Send Test Message

5.  On clicking Send test message button a window will open where we have to add device FCM tokens on which we have to send push notification.

Add Token

6. After adding device tokens, select the tokens on which you want to send the push notification and click on the Test button.

Select Tokens

7.  On clicking of Test button your device will receive the notification only if your app is in the background or killed state.

Receiving Notification in Background

Receiving Push Notification

1. To receive the notification first thing we need to add the action in AndroidManifest.xml as below:

By adding com.google.firebase.MESSAGING_EVENT the device will receive all the incoming push notifications.

2. FCM SDK will automatically handle the notification when the app is in the background/killed state. To handle the notification when the app is in the foreground/running then we have to add the following code to MyFirebaseMessagingService.java class.

Here we are reading notification from the RemoteMessage parameter.

3. Now we have to use the NotificationCompatBuilder to create notification and display in the notification tray. Use the below method to show the notification.

4. Now we have to create NotificationChannel so that notification will work on Oreo+ API Level devices. Use the below code to do so.

Here you have to define your channel id which should be unique for every notification channel. Same channel id we have to use while showing a notification.

If you will not create the notification channel then the notification will not be displayed on Oreo+ devices.

5. Now if we run the app and send the push notification we can receive the notification in the foreground/running state as well.

Receiving Notification in Foreground

Sending to User Segment

If we have to send the notification to all the users in the application then follow the below steps:

1. Go to the Target section and select the User Segment from it.

User Segment

2. Under User Segment select the connected apps package name on which you have to send the push notification and you are good to send the notification to all the users on that selected application.

Select App

Sending Data Payload

To send the Data payload push notification follow the below steps:

1. Follow 1-3 steps from Sending Notification Payload.

2. After that go to Additional options and add the key-value data in the Custom data section.

Data Payload

3. After that click on the Review button to review the notification before sending it.

Review Notification

4. On clicking the Review button it will open a window where you can review the content and click on the Publish button if everything looks good.

Publish Notification

On clicking Publish button it will send the push notification to the client.

Handling Data Payload Notification

To handle the data payload notification on the client-side follow the below steps:

1. Add the below code to onMessageReceived() method of MyFirebaseMessaging.java class.

Here you will get the data payload from RemoteMessage which is in Map<String, String> data type. You can iterate the Map to get the key and values and show the notification if required.

2. If your payload consists of both notification and data payload and your app is in background/killed state then your onMessageReceived method will not receive a callback so in that case, if we have to get the data content then we can get it via getIntent() in the launching activity.

Put the above code in your launching activity onCreate() method and you can read the data content as well.

Topic Messaging

FCM Topic messaging is based on the publish/subscribe model. By using Topic we can send the notification to multiple devices that have subscribed to the topic.

For example, If we have an e-commerce application and we have to send the promotional notification to the users then instead of sending via user tokens we can use topics to send the notification.

To send the Topic notification select the Topic under the Target section and enter your topic name to send it.

Topic Notification

Subscribe to Topic: You can use the following code to subscribe to the client app to any topic.

UnSubscribe to Topic: You can use the following code to subscribe to the client app to any topic.

In both codes, the topic is the name of the topic for which the client can subscribe/unsubscribe.

The JSON structure of Topic is the same as we have for Notification and Data Payload. The only difference is instead of the token as the key we have the topic as key.

Note:

1. Topic name will be of String type.

2. If the topic name does not exist in Firebase for the project then it will create it and if the topic name exists then it will not create it.

3. There is no way to check if the device is already subscribed to topic or not. For that, we have to use internal mechanisms like Shared Preferences or any other mechanism.

4. To handle the topic notification on client side the procedure is same which we have seen above for data and notification payload.

Thanks. 🙂

 

2 Comments

Shiva
Wednesday, April 14th, 2021

getToken() not working in latest version can you solve.

Dr. Droid
Monday, April 26th, 2021

Hi Shiva,

Please check the 1st part of this article: https://www.androhub.com/setup-firebase-cloud-messaging-in-android-part-1/.

Thanks

Post comment

Your email address will not be published. Required fields are marked *