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