Sending Data From Backend To Android - Huawei Push Kit vs. FCM (Android and Server-side)
Introduction
Hi folks :)
I will introduce you to how to send data from the server-side to Android via Huawei Push Kit & Firebase Cloud Messaging both Android side and server-side. Also, I will show you how to send push notification from backend, catch Android side and open specific page on Android app.
This article includes two titles, FCM and Huawei Push Kit. Although both have different uses, I will try to explain how to use them in a similar scenario.
FCM
Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost.
Server Side
Here is PushService interface.
The sendFCMNotification method will be sufficient to send notifications.
FCMNotificationResponse class is required to see a response after the notification request is sent.
I’m just checking the success and failure variables. Those variables are enough to know the status of the request. If you need more information, you can take a look at other variables.
FCMNotification object is used as a body when calling HTTP Request for sending notifications.
to: The push token of the user to send the notification.
FCMData: Data to be sent to the Android side. Each data was defined as a separate variable. series_Id and series_title variables are used within the app. The notification_body and notification_title variables will be used when creating the Notification object on the Android side.
In the FCMProcess class, creating the Retrofit object and sending notification via FCM.
Where to get the FCM_SERVER_KEY value?
FCM_SERVER_KEY coming from Firebase.
You can access the key: Firebase > Project Settings > Cloud Messaging
Suppose the key you got from Firebase is “xxx” the FCM_SERVER_KEY’s value must be like this: “key=xxx”
Android Side
First of all, you must integrate Firebase into the project. I will not explain these steps as they will get out of the purpose of the article.
There are FCM implementation lines.
I recommend you to check if the version you use is up to date.
default_notification_icon: Set custom default icon. This is used when no icon is set for incoming notification messages.
default_notification_color: Set color used with incoming notification messages. This is used when no color is set for the incoming notification message.
default_notification_channel_id: From Android 8.0 (API level 26) and higher, notification channels are supported and recommended. FCM will use this value whenever incoming messages do not explicitly set a notification channel.
MyFirebaseMessagingService: The service that extends FirebaseMessagingService. This is required if you want to do any message handling beyond receiving notifications on apps in the background. To receive notifications in foregrounded apps, to receive data payload, to send upstream messages, and so on, you must extend this service.
When the device receives a notification, the onMessageReceived method is triggered.
On the showNotification method, The data to be sent to the intent is received from the remoteMessage, The notification object is created with the notification_body and notification_title values from the remoteMessage object.
The notification channel value will be set after the condition is true. And than notification will be displayed.
The data added to the intent in the MyFirebaseMessagingService class is taken as extra. After this step, those values can be used within the app.
I usually use these values to navigate within the app.
Huawei Push Kit
Push Kit is a messaging service provided for you to establish a cloud-to-device messaging channel. By integrating Push Kit, you can send messages to your apps on user devices in real time. This helps you maintain closer ties with users and increases user awareness of and engagement with your apps.
Server Side
Here is PushService interface.
The getAccessToken and sendNotification methods will be sufficient to send notifications.
CLIENT_ID, CLIENT_SECRET as “App ID” and “App secret” data available on Huawei AGC.
To get the access_token used when sending push notifications via Huawei Push Kit AccessToken class is required.
PushNotificationResponse class is required to see a response after the notification request is sent.
PushNotification object is used as a body when calling HTTP Request for sending notifications.
token: Push token of the target user of a message.
urgency: Delivery priority of a data message. The options are as follows:
- HIGH
- NORMAL
collapse_key: Mode for the HUAWEI Push Kit server to control messages cached in user offline status. These cached messages will be delivered once the user device goes online again.
- 0: Only the latest offline message sent by each app to the user device is cached.
- -1: All offline messages are cached.
title: Android notification message title.
body: Android notification message body.
type: Message tapping action type.
- 1: custom tapping action.
- 2: tap to open a specified URL.
- 3: tap to start the app.
- 4: tap to access rich media information.
intent: The data to be used on the Android side is sent with this variable. The values of the variables are below.
There is more detail about the PushNotification object: link
In the AccessTokenProcess class, Retrofit object created and notification sent via Huawei Push Kit.
I am using this class as a Cron Job. Before each request, instead of getting the access_token from the Huawei Push Kit, I take the access_token value in the range I specified and update it in the database.
In the PushNotificationProcess class, Retrofit object created and notification sent via Huawei Push Kit.
Android Side
First of all, you must integrate HMS into the project. I will not explain these steps as they will get out of the purpose of the article. You can look at this story.
There is just one line on Push Kit implementation.
I recommend you to check if the version you use is up to date.
The Service tag needs to be added to the Android.Manifest file for the Push Service.
Also, the data tag should be added inside the intent-filter tag to handle the data coming from the server-side.
Here is PushService class.
PushService is mandatory for getting the push_token.
Unlike FCM, in the MainActivity, no data is set to intent in PushService class. Data from the server side can be accessed directly using intent.getExtra method.
Congratulations, you can now use the data from the server side on the Android side.
Tips & Tricks
- In FCM, server_key is generated one time and you can use it without any time limit.
- Huawei Push Kit has the Auth system. Before sending notifications, you need to get access_token from Huawei Push Kit.
- When using the Huawei Push Kit, no control is required in the PushService class, the notification is displayed directly on the screen.
- Don’t forget to add “key=” to the beginning of the Server Key you get from Firebase.
Conclusion
In this article, I tried to explain how to use Huawei Push Kit & FCM in a similar scenario. I hope it was a useful article for everyone. Thanks for taking the time to read.
Hope to see you in my next article :)