Home
/
Calls
/
Android

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 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.


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

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.

  1. Go to the Firebase console. If you don't have a Firebase project for your app, create a new project.
  2. Select your project card to move to the Project Overview.
  3. Click the gear icon at the upper left corner and select Project settings.
  4. Go to Cloud Messaging > Project credentials and copy your server key.
  5. 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:

  1. Sign in to your dashboard and go to Settings > Calls > Notifications.
  2. 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:

build.gradle
Light Color Skin
Copy
dependencies {
    ...

    implementation 'com.google.firebase:firebase-messaging:20.1.0'
}

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.

MyFirebaseMessagingService.java
Light Color Skin
Copy
@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.
    });
}

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.

Light Color Skin
Copy
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
    if (SendBirdCall.handleFirebaseMessageData(remoteMessage.getData())) {

    } else {
        // Handle push notifications which are not Sendbird Calls push notifications.
    }
}