Notifications
A push notification is a message that is immediately delivered to a user device when the device is either idle or running your app in the background. Push notifications for Android client apps are sent using Firebase Cloud Messaging (FCM) and Huawei Mobile Services (HMS) which include custom data your app needs to respond to the notifications. When a call is made to Sendbird server through our Calls SDK for Android, the server communicates with FCM and HMS that will deliver a push notification to an Android device where your app is installed.
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
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 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 > Calls > Notifications.
- Click the Add credentials button and register the app ID and app secret acquired at Step 1.
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: 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 your 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 SendBirdCall.registerPushToken()
method as shown below.
Step 5 Handle an FCM message payload
To learn more about how to implement code 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, refer to Firebase's Receive messages in an Android app guide.
Since different push notifications are received from the Calls SDK or your app through the onMessageReceived()
method, you first need to pass the payload to the Calls SDK as shown below to specify which push notifications are from Sendbird Calls. If true
, the Calls SDK will parse the payload and otherwise, false
will be returned.
Push notifications for HMS
The following is a set of step-by-step instructions on how to set up push notifications for HMS.
-
Step 2: Register app ID and app secret to Sendbird Dashboard
-
Step 4: Register a registration token to the Sendbird server
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.
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 Project Settings.
-
Go to Convention > App Information and copy your App ID and App secret. You will need them on Sendbird Dashboard in Step 2.
- During the registration process, enter your package name, download the
agconnect-services.json
file, and place it in your Android app module's root directory.
Step 2 Register app ID and app secret to Sendbird Dashboard
Register your app ID and app secret to the Sendbird server through Sendbird Dashboard as follows.
-
Sign in to your dashboard and go to Settings > Calls > Push notifications.
-
Click Add credentials and register the App ID and App secret acquired in 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 your build.gradle
files at both the project
and app
levels.
Step 4 Register a registration token to the 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, the Sendbird server also needs every registration token of client app instances to send notification requests to HMS on behalf of your server.
A user can have up to 20 HMS registration tokens. If a user who already has the maximum number of tokens attempts to add another one, the newest token replaces the oldest.
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 has successfully generated the registration token, it is passed to the onNewToken()
callback. Registration tokens must be registered to the Sendbird server by passing it as an argument to the parameter in the SendbirdCall.HMS.registerPushToken()
method as in the following code.
Note: If
PushTokenRegistrationStatus.PENDING
is returned through the handler, this means that your user isn't being connected to the Sendbird server whenSendbirdCall.HMS.registerPushToken()
is called. In this case, you must first get a pending registration token usinggetToken()
, and then register the token by callingSendbirdCall.HMS.registerPushToken()
in theonSuccess()
callback when your user has been connected to the server.
When the server fails to return a token after a client app has called the getToken()
method, it tries to return the token through the onNewToken()
method instead as in the following scenario.
- After the server has 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 has 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
Different push notifications can be received from both Sendbird Calls SDK and your app through the onMessageReceived()
method. To determine which push notifications are from Sendbird Calls, pass the notification payload to the Calls SDK as shown in the snippet below. If true
, the Calls SDK parses the payload.
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
onMessageReceived()
method.
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.