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 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 then they deliver a push notification to an Android device where your app is installed.
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:
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.
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.
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.
@Override
public void onNewToken(@NonNull String token) {
SendBirdCall.registerPushToken(token, false, e -> {
if (e != null) {
// Handle error.
return;
}
// Registration tokens successfully registered to Sendbird server.
});
}
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.
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
if (SendBirdCall.handleFirebaseMessageData(remoteMessage.getData())) {
} else {
// Handle push notifications which are not Sendbird Calls push notifications.
}
}