Push notifications
A push notification is a message that is immediately delivered to a user's device when the device is either idle or running the client app in the background. Push notifications for Android client apps are sent using Firebase Cloud Messaging (FCM) or Huawei Mobile Service (HMS), which contains custom data that your app needs in order to respond to the notifications. When a message is sent to Sendbird server through our Chat SDK for Android, the server communicates with FCM or HMS, and then they deliver a push notification to an Android device where the client app is installed.
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 explicitly call the
disconnect()
method.
Notifications can also be configured to display an alert, play a sound, or badge your app’s icon and are only applicable to group channels. They can't be applied to an open channel or a supergroup channel due to the massive number of users and messages.
Push notifications support both single and multi-device users and they are delivered only when a user is fully offline from all devices even when they use only one device. In other words, if a user is online on one or more devices, notifications aren't delivered and thus not displayed on any devices.
Sendbird provides two options for push notifications. Choose an appropriate option upon consideration of how much support for multi-device push notifications the client app requires. Push notifications with Multi-Device support deliver notifications to all offline devices even when a user is online on one or more devices. Refer to Understanding the differences in the Multi-Device Support page to learn more about the difference between the two options in detail.
Push notifications for FCM
There are two types of FCM messages: notification messages and data messages. Sendbird uses data messages, which are handled by the client app. They allow users to customize the message payload, which consists of key-value items.
The following is a set of step-by-step instructions on how to set up push notifications for FCM:
- Step 1: Generate server key for FCM
- Step 2: Register server key to Sendbird Dashboard
- Step 3: Set up an FCM client app on your Android project
- Step 4: Register a registration token to Sendbird server
- Step 5: Handle an FCM message payload
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 a 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.
Note: If a server key wasn't generated because the Cloud Messaging API is disabled by default, see how to enable the legacy API.
- 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 the dashboard as follows:
- Sign in to your dashboard and go to Settings > Chat > 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.
Note: Your server key can also be registered using our Chat Platform API's add an FCM push configuration action.
Step 3: Set up an FCM client app on your Android project
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.
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: Register a registration token to Sendbird server
In order to send notification messages to a specific client app on an Android device, FCM requires an app instance's registration token which has been issued by the client app. Therefore, Sendbird server also needs every registration token of client app instances to send notification requests to FCM on behalf of your server.
Note: A user can have up to 20 FCM registration tokens. If a user who already has the maximum number of tokens adds another one, the newest token will push out the oldest, meaning the oldest token will be deleted to add the newest.
Upon the initialization of your app, the FCM SDK generates a unique, 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. After the FCM SDK successfully generates the registration token, it is passed to the onNewToken()
callback. Registration tokens must be registered to Sendbird server by passing it as an argument to the parameter in the registerPushTokenForCurrentUser()
method as shown below.
Note: If
SendBird.PushTokenRegistrationStatus.PENDING
is returned through the handler, this means that your user is not being connected to Sendbird server when theregisterPushTokenForCurrentUser()
is called. In this case, you must first get a pending registration token using thegetInstanceId()
, and then register the token by calling theregisterPushTokenForCurrentUser()
in theonSuccess()
callback when your user has been connected to the server.
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.
Push notifications for HMS
The following is a set of step-by-step instructions on how to set up 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 an HMS client app on your Android project
- Step 4: Register a registration token to Sendbird server
- Step 5: Handle an HMS message payload
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 a 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 > Chat > Notifications.
- Turn on Notifications and select Send when all devices are offline.
- Click the Add credentials button and register the app ID and app secret acquired at Step 1.
Step 3: Set up an HMS client app on your Android project
Add the following dependency for the HUAWEI Push Kit Android library to yourbuild.gradle
files that are at the project level and app level.
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: Register a registration token to Sendbird server
In order to send notification messages to a specific client app on an Android device, HMS requires an app instance's registration token which has been issued by the client app. Therefore, Sendbird server also needs every registration token of client app instances to send notification requests to HMS on behalf of your server.
Note: A user can have up to 20 HMS registration tokens. If a user who already has the maximum number of tokens adds another one, the newest token will push out the oldest, meaning the oldest token will be deleted to add the newest.
Upon the initialization of your app, the HMS SDK generates a unique, 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. After the HMS SDK successfully generates the registration token, it is passed to the onNewToken()
callback. Registration tokens must be registered to Sendbird server by passing it as an argument to the parameter in the SendBird.HMS.registerPushTokenForCurrentUser()
method as shown below.
Note: If
SendBird.PushTokenRegistrationStatus.PENDING
is returned through the handler, this means that your user is not being connected to Sendbird server when theSendBird.HMS.registerPushTokenForCurrentUser()
is called. In this case, you must first get a pending registration token using thegetToken()
, and then register the token by calling theSendBird.HMS.registerPushTokenForCurrentUser()
in theonSuccess()
callback when your user has been connected to the server.
When the server fails to return a token after a client app called the getToken()
method, it tries to return the token through the onNewToken()
method instead. as the following scenario.
- After the server failed to return a token, HUAWEI Push Kit automatically calls the method again, then the server returns the requested token through the
onNewToken()
method. - If the requested token is expired, the server returns the updated token through the
onNewToken()
method. - When the EMUI version of a Huawei device is lower than 10.0, the server returns the token through the
onNewToken()
method.
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.
Notification preferences
By registering or unregistering the current user's registration token to Sendbird server as below, you can turn push notifications on or off in the user's client app.
Note: The registration and unregistration methods in the code below should be called after a user has established a connection with Sendbird server by calling the
SendBird.connect()
method.
The SendBird.PushTriggerOption
allows users to configure when to receive notification messages as well as what type of messages will trigger notification messages. This provides the following three options:
Option | Description |
---|---|
ALL | When disconnected from Sendbird server, the current user receives notifications for all new messages including messages the user has been mentioned in. |
MENTION_ONLY | When disconnected from Sendbird server, the current user only receives notifications for messages the user has been mentioned in. |
OFF | The current user doesn't receive any notifications. |
The GroupChannel.PushTriggerOption
also allows users to configure the trigger for notification messages as well as turn notifications on or off for each channel. This provides the following four options:
List of push trigger options
Option | Description |
---|---|
DEFAULT | The current user’s push notification trigger settings are automatically applied to the channel. This is the default setting. |
ALL | When disconnected from Sendbird server, the current user receives notifications for all new messages in the channel including messages the user has been mentioned in. |
MENTION_ONLY | When disconnected from Sendbird server, the current user only receives notifications for messages in the channel the user has been mentioned in. |
OFF | The current user doesn't receive any notifications in the channel. |
If you want to routinely turn off push notification on the current user's client app according to a specified schedule, use our Do Not Disturb
feature like the following.
Note:
Do Not Disturb
can also be set for a user with our Chat Platform API's update push notification preferences action.
To snooze notification messages for a specific period of time, use our snooze
feature as below.
Note:
snooze
notifications can also be set for a user with our Chat Platform API's update push notification preferences action.
Push notification content templates
Push notification content templates are pre-formatted forms that can be customized to display your own push notification messages on a user’s device. Sendbird provides two types: default and alternative. Both templates can be customized from your dashboard by going to Settings > Chat > Notifications > Push notification content templates.
As for Android, an additional implementation is required in order to display your customized push notifications. The push notification payload sent from Sendbird server will include content of the template, such as the title and message, and the client app can determine how to present the notification through codes provided by Android. For more information on how to create a custom notification, refer to Notifications at Android's Documentation for app developers.
Huawei Mobile Service (HMS) also requires an additional implementation for custom push notifications. To learn more, see Push Kit at Huawei's Documentation.
Content templates
There are two types of push notification content template: Default and Alternative. Default template is a template that applies to users with the initial notification message setting. Alternative template is used when a user selects a different notification content template instead of the default template.
The content in the template is set at the application level while the selection of templates is determined by a user or through the Platform API.
Note: When a custom channel type has its own customized push notification content template, it takes precedence over the default or alternative templates.
Both templates can be customized using the variables of sender_name
, filename
, message
, channel_name
, and file_type_friendly
which will be replaced with the corresponding string values. As for the file_type_friendly
, it will indicate the type of the file sent, such as Audio, Image, ans Video.
List of content templates
Refer to the following table for the usage of content templates:
Text message | File message | |
---|---|---|
Default template | {sender_name}: {message} (ex. Cindy: Hi!) | {filename} (ex. hello_world.jpg) |
Alternative template | New message arrived | New file arrived |
To apply a template to push notifications, use the setPushTemplate()
method. Specify the type of the template with the name as either SendBird.PUSH_TEMPLATE_DEFAULT
or SendBird.PUSH_TEMPLATE_ALTERNATIVE
.
To check the current user's push notification template, use the getPushTemplate()
method as follows:
Push notification tester
After registering a credential for push notification services, you can test whether push notification credentials and notification services work properly through the Sendbird Dashboard.
In Settings > Notifications > Push notification credentials on the dashboard, each credential has a Send button that allows you to send a test message. The test message will be sent to a target user and their device, which sets off a push notification on the device. At the same time, a new group channel will be created for this test, containing only the target user. If you've already tested push notification with the same target user and the same target device token, the message will be sent to the existing test channel.
Note: If there is another member in the test channel or the target user has left the channel, delete the test channel before carrying out a test.
- Go to Settings > Notifications > Push notification credentials on Sendbird Dashboard. Find a Send button in the right-end column of each table.
- You can search a target user by either their User ID or Nickname in the popup window. Once a user is selected, choose a target device from a list of device tokens linked to the user.
- Double check to see if you've selected a correct device token of the user because this push notification will show up on an actual device. Confirm the target channel name and URL, then send the message.
Note: If this is your first test, a new test channel will be created. For future tests with the same target user and the same target device token, the name and URL of the existing test channel will show above the message box.
- Check to see if the push notification arrived on the mobile device or emulator. The test message will state the time when Sendbird server sent the message.
Note: In order to display push notifications on Android devices and emulators, an additional implementation is required in the client app. To learn more, see Push notification for FCM or Push notification for HMS.
Push notification translation
Push notification translation allows your users to receive notification messages in their preferred languages. Users can set up to four preferred languages. If messages are sent in one of those languages, the notification messages won’t be translated. If messages are sent in a language other than the preferred languages, the notification messages will be translated into the user's first preferred language. In addition, the messages translated into other preferred languages will be provided in the sendbird
property of a notification message payload.
Note: A specific user's preferred languages can be set with our Chat Platform API's update a user action.
The following shows how to set the current user's preferred languages using the updateCurrentUserInfo()
method:
If successful, the following notification message payload will be delivered to the current user's device.
Enable Cloud Messaging API
To enable the legacy Cloud Messaging API, take the following steps.
Step 1 Open Firebase console
In the Firebase console, go to Project settings > Cloud Messaging and select Manage API in Google Cloud Console to open Google Cloud Console.
Step 2 Go to API Library
Move to API Library by using the back button as shown below.
Step 3 Find Cloud Messaging API
In the search bar, type "cloud messaging."
In the search results, select Cloud Messaging as shown below.
Step 4 Enable Cloud Messaging API
Click the Enable button to start using the Cloud Messaging API.
Step 5 Check that the legacy API is enabled
If you go back to your Firebase console, the Cloud Messaging API should be enabled with a newly generated server key.