Set up push notifications for HMS
This part covers the following step-by-step instructions of our push notifications for HMS.
Note: See Set up push notifications for FCM if you want to learn how to set up notifications for FCM.
Step 1: Generate app ID and app secret for HMS
The Sendbird server requires your app ID and app secret to send notification requests to HMS on behalf of your server. This is required for HMS to authorize HTTP requests.
If you already have your app ID and app secret, you can skip this step and go directly to Step 2: Register app ID and app secret to Sendbird Dashboard.
- Go to the AppGallery Connect. If you don't have a project for your client app, create a new project.
Select your project card to move to Project Settings.
Go to Convention > App Information and copy your App ID and App secret to use them on Sendbird Dashboard later.
- During the registration process, enter your package name, download the
agconnect-services.jsonfile, and place it in your Android app module root directory.
Step 2: Register app ID and app secret to Sendbird Dashboard
Register your app ID and app secret to Sendbird Dashboard as follows.
Sign in to your dashboard and go to Settings > Chat > Push notifications.
Turn on Push notifications and select Send to devices when all offline.
Click Add credentials and register the app ID and app secret acquired at Step 1.
Step 3: Set up Huawei Message Service and the HMS SDK
Add the following dependency for the Cloud Messaging Android library to your
build.gradle files that are at the project level and app level.
Then the Chat SDK writes and declares our push notifications with multi-device support in the manifest while you build your client app. If you declare another push service that extends FirebaseMessagingService in your client app's manifest, this multi-device support doesn't work in the app.
Step 4: Implement multi-device support in your Android app
The following classes and interface are provided to implement push notifications with multi-device support.
|Class or interface||Description|
A class that provides the
A class that provides the methods to register and unregister a
An interface that contains the
These are used to inherit your
MyHmsMessagingService class from the
SendbirdHmsPushHandler class and implement the following.
Note: Upon initial startup of your app, the HMS SDK generates a unique and app-specific registration token for the client app instance on your user's device. HMS uses this registration token to determine which device to send notification messages to.
In order to receive information about push notification events for the current user from the Sendbird server, register a
MyHmsMessagingService instance to
SendbirdPushHelper as an event handler.
To receive a push notification when the app is in the background or closed, you must register the
MyHmsMessagingService instance. This instance isn't dependent on Sendbird Chat SDK, so even in cases where
Sendbird.init() isn't called in
MyHMSMessagingService must be registered in the
onCreate() method as shown in the code below.
Also, register a
MyHmsMessagingService instance when a user logs into the Sendbird server as follows.
The instance should be unregistered when a user logs out from the Sendbird server as follows.
Step 5: Handle an HMS message payload
The Sendbird server sends push notification payloads as HMS notification messages, which contain notification-related data in the form of key-value pairs. Unlike notification messages, the client app needs to parse and process these data messages in order to display them as local notifications.
The following code shows how to receive a push notification payload and parse it as a local notification. The payload consists of two properties:
message property is a string generated according to a notification template you set on the Sendbird Dashboard. The
sendbird property is a
JSON object which contains all the information about the message a user has sent. Within
MyFirebaseMessagingService.java, you can show the parsed messages to users as a notification by using your custom
Note: See Huawei’s Receive messages in an Android app guide to learn more about how to implement code to receive and parse an HMS notification message, how notification messages are handled depending on the state of the receiving app, how to edit the app manifest, and how to override the
The following is a complete payload format of the
sendbird property, which contains a set of provided key-value items. Some fields in the push notification payload can be customized in Settings > Chat > Push notifications on Sendbird Dashboard. For example,
push_alert are created based on the Title and Body text you set in Push notification content templates, respectively, in the Push notifications menu. In order to display them in a local notification, pass
push_alert of the push notification payload into the
setContentText methods of the
NotificationCompat.Builder class, respectively. Also, the
channel_unread_count field can be added into or removed from the payload in the same menu on the Sendbird Dashboard.
Step 6: Enable multi-device support on Sendbird Dashboard
After the above implementation has been completed, multi-device support should be enabled on Sendbird Dashboard by going to Settings > Application > Notifications > Push notifications for multi-device users.