Push notifications with multi-device support
Sendbird’s push notifications with multi-device support provide the same features as our general push notifications, but with additional support for multi-device users. If implemented, notifications are delivered to all online and offline devices of a multi-device user. However, through our Chat SDK for Android, push notifications are displayed only on offline devices, while ignored by online devices. As a result, client apps are able to display push notifications on all offline devices, regardless of whether one or more are online.
For example, let’s say a multi-device user who has six devices installed with your client app is online on one device and offline on the remaining five. If push notifications are implemented, notifications aren’t delivered to any devices. If push notifications with multi-device support are implemented, notifications are delivered to all six devices, but displayed only on the five devices that are offline.
Note: By default, when a user's device is disconnected from Sendbird server, the server sends push notification requests to FCM for the messages. The Chat SDK for Android automatically detects when a user's client app goes into the background from the foreground, and updates the user's connection status to disconnected. Therefore, under normal circumstances, you don't need to call the
disconnect()
method.
Understanding the differences
To find out which push notifications best suits your client app use cases, refer to the table below:
General push notifications | Push notifications with multi-device support | |
---|---|---|
Send when | All devices are offline. | As long as one device is offline. |
Notification messages | Single-device user: Displayed when disconnected from the server and thus offline. | Single-device user: Displayed when disconnected from the server and thus offline. |
SDK's event callback | Invoked only when the app is connected to Sendbird server. | Invoked only when the app is connected to Sendbird server. |
App instance's registration token | Can be manually registered to Sendbird server. | Automatically registered to Sendbird server. |
Notification preferences | Can be set by application and group channel. | N/A |
Push notification templates | Supported. | Supported. |
Push notification translation | Supported by Google Cloud Translation API. | Supported by Google Cloud Translation API. |
Push notifications for FCM
This part covers the following step-by-step instructions of our push notifications for FCM:
- Step 1: Generate server key for FCM
- Step 2: Register server key to Sendbird Dashboard
- Step 3: Set up Firebase and the FCM SDK
- Step 4: Implement multi-device support in your Android app
- Step 5: Handle an FCM message payload
- Step 6: Enable multi-device support in the Dashboard
Note: Move to Push notifications for HMS if you want to see the instructions for HMS push notifications.
Step 1: Generate server key for FCM
Sendbird server requires your server key to send notification requests to FCM on behalf of your server. This is required for FCM to authorize HTTP requests.
Note: If you already have your server key, skip this step and go directly to Step 2: Register server key to Sendbird Dashboard.
- Go to the Firebase console. If you don't have a Firebase project for your client app, create a new project.
- Select your project card to move to the Project Overview.
- Click the gear icon at the upper left corner and select Project settings.
- Go to Cloud Messaging > Project credentials and copy your server key.
- Go to the General tab and select your Android app to add Firebase to. During the registration process, enter your package name, download the
google-services.json
file, and place it in your Android app module root directory.
Step 2: Register server key to Sendbird Dashboard
Register your server key to Sendbird server through your dashboard as follows:
- Sign in to your dashboard and go to Settings > Application > Notifications.
- Turn on Notifications and select the
Send as long as one device is offline
option. - Click the Add credentials button and register the app ID and app secret acquired at Step 1.
Note: Your server key can also be registered using the platform API's add an FCM push configuration action.
Step 3: Set up Firebase and the FCM SDK
Add the following dependency for the Cloud Messaging Android library to your build.gradle
file as below:
Note: The firebase-messaging version should be 19.0.1 or higher.
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 will not work in the app.
Note: To learn more about this step, refer to Firebase's Set Up a Firebase Cloud Messaging client app on Android guide. The Google FCM sample project is another helpful reference.
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 |
---|---|
SendBirdPushHandler | A class that provides the |
SendBirdPushHelper | A class that provides the methods to register and unregister a |
OnPushTokenReceiveListener | An interface that contains the |
These are used to inherit your MyFirebaseMessagingService
class from the SendBirdPushHandler
class and implement the following:
Note: Upon initial startup of your app, the FCM SDK generates a unique and app-specific registration token for the client app instance on your user's device. FCM 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 Sendbird server, register a MyFireBaseMessagingService
instance to the SendBirdPushHelper
as an event handler. It is recommended to register the instance in the onCreate()
method of the Application
instance as follows:
Also, register a MyFireBaseMessagingService
instance when a user logs into Sendbird server as follows:
The instance should be unregistered when a users logs out from Sendbird server as follows:
Step 5: Handle an FCM message payload
Sendbird server sends push notification payloads as FCM data messages, which contain notification-related data in the form of key-value pairs. Unlike notification messages, the client app needs to parse and process those data messages in order to display them as local notifications.
The following code shows how to receive a Sendbird’s push notification payload and parse it as a local notification. The payload consists of two properties: message
and sendbird
. The 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 the MyFirebaseMessagingService.java
, you can show the parsed messages to users as a notification by using your custom sendNotification()
method.
Note: Visit Firebase’s Receive messages in an Android app guide to learn more about how to implement codes to receive and parse a FCM notification message, how notification messages are handled depending on the state of the receiving app, how to edit the app manifest, or how to override the
onMessageReceived
method.
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 > Notifications on the Sendbird Dashboard. For example, the push_title
and push_alert
are created based on the Title and Body text you set in Push notification content templates, respectively, in the Notifications menu. In order to display them in a local notification, pass the push_title
and push_alert
of the push notification payload into the setContentTitle
and setContentText
method 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 in the Dashboard
After the above implementation is completed, multi-device support should be enabled in your dashboard by going to Settings > Application > Notifications > Push notifications for multi-device users.
Push notifications for HMS
This part covers the following step-by-step instructions of our push notifications for HMS:
- Step 1: Generate app ID and app secret for HMS
- Step 2: Register app ID and app secret to Sendbird Dashboard
- Step 3: Set up Huawei Message Service and the HMS SDK
- Step 4: Implement multi-device support in your Android app
- Step 5: Handle an HMS message payload
- Step 6: Enable multi-device support in the Dashboard
Note: Move to Push notifications for FCM if you want to see the instructions for FCM push notifications.
Step 1: Generate app ID and app secret for HMS
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.
Note: If you already have your app ID and app secret, 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 the Project Settings.
- Go to Convention > App Information and copy your App ID and App secret to use them in your Sendbird Dashboard later.
- During the registration process, enter your package name, download the
agconnect-services.json
file, 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 server through the dashboard as follows:
- Sign in to your dashboard and go to Settings > Application > Notifications.
- Turn on Notifications and select the
Send when all devices are offline
option. - Click the Add credentials button 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 will not work in the app.
Note: To learn more about this step, refer to Huawei's Preparations guide. The Push Kit sample code for Android is another helpful reference.
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 |
---|---|
SendBirdHmsPushHandler | A class that provides the |
SendBirdPushHelper | A class that provides the methods to register and unregister a |
OnPushTokenReceiveListener | 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 Sendbird server, register a MyHmsMessagingService
instance to the SendBirdPushHelper
as an event handler. It is recommended to register the instance in the onCreate()
method of the Application instance as follows:
Also, register a MyHmsMessagingService
instance when a user logs into Sendbird server as follows:
The instance should be unregistered when a users logs out from Sendbird server as follows:
Step 5: Handle an HMS message payload
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 those data messages in order to display them as local notifications.
The following code shows how to receive a Sendbird’s push notification payload and parse it as a local notification. The payload consists of two properties: message
and sendbird
. The 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 the MyFirebaseMessagingService.java
, you can show the parsed messages to users as a notification by using your custom sendNotification()
method.
Note: Visit 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
onMessageReceived()
method.
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 > Notifications on the Sendbird Dashboard. For example, the push_title
and push_alert
are created based on the Title and Body text you set in Push notification content templates, respectively, in the Notifications menu. In order to display them in a local notification, pass the push_title
and push_alert
of the push notification payload into the setContentTitle
and setContentText
method 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 in the Dashboard
After the above implementation is completed, multi-device support should be enabled in your dashboard by going to Settings > Application > Notifications > Push notifications for multi-device users.