A push notification is a message immediately delivered to a user’s device when the device is either idle or running the client app in the background. Push notifications are important because they deliver important information, updates, and increase user engagement with your app. Moreover, push notifications are a powerful marketing channel by which you can target the right users, deliver meaningful content, increase conversion rates, and enhance your customer care strategy. In fact, according to RubyGarage, organizations that implement push notifications have 3x higher customer retention and 88% higher engagement than businesses that do not! Our guide to mobile push notifications has everything you need to know about push notifications, their benefits, and how they can be used.
This tutorial provides step-by-step instructions for implementing Android push notifications.
It is important to understand that push notifications on Android client apps are sent using Firebase Cloud Messaging (FCM) or Huawei Mobile Service (HMS), and these contain custom data that your app needs to respond to the notifications. When a message is sent to the Sendbird server from an app (through the Chat SDK for Android), the server communicates with either FCM or HMS, and then FCM or HMS delivers a push notification to the Android device. This article focuses on setting up push notifications with FCM.
Sendbird provides two options for push notifications: general push notifications and push notifications with multi-device support.
General push notifications support both single and multi-device users and are delivered only when a user is fully offline (disconnected from the server) from all devices. In other words, if a user is online on one or more devices, notifications aren’t delivered and thus not displayed on any device. On the other hand, push notifications with multi-device support are delivered to all offline devices, even when a user is online on one or more devices.
There are two types of FCM messages: notification messages and data messages. According to the Firebase documentation, “Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.” In contrast, data messages contain only user-defined key-value pairs. Sendbird uses data messages, allowing client apps to create the custom message payload consisting of these custom key-value pairs.
Follow the below steps to set up the push notifications for FCM. Before you start, ensure that you have access to the Firebase console.
Let’s dive in! 💻
The Sendbird server requires an FCM server key to send notification requests to FCM on your behalf. You can view your FCM server key as follows:
Register your server key to the Sendbird server through the dashboard as follows. Note that the server key can also be registered using the Chat Platform API’s add an FCM push configuration action.
Now we will add the FCM configurations and dependencies to your Android project.
dependencies { ... implementation 'com.google.firebase:firebase-messaging:20.1.0' }
To send notification messages to a specific client app on an Android device, FCM requires an app instance’s registration token, which the client app has issued. Therefore, the Sendbird server also needs the registration token of every client app instance to send notification requests to FCM on your behalf.
Upon your app’s initialization, 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 the device to which notification messages need to be sent. You can access this token by extending ‘com.sendbird.android.SendBirdPushHandler’ and overriding ‘onNewToken’. The ‘SendBirdPushHandler’ internally registers this token to the Sendbird server.
Here is the sample implementation:
The registered push token appears in the dashboard under ‘Users > user_id > Chat’:
To receive information from the Sendbird server about push notification events for the current user, register a ‘MyFireBaseMessagingService’ instance to the ‘SendBirdPushHelper’ as an event handler. Do this in the ‘onCreate()’ method of the ‘Application’ instance as follows:
Also, register a ‘MyFireBaseMessagingService’ instance when a user connects to the Sendbird server as follows:
Also, before disconnecting the user from the Sendbird server, unregister all the push tokens so that they do not receive any notifications.
As described earlier, the Sendbird server sends push notification payloads as FCM data messages. The payload consists of two properties: message and sendbird. The message property is a string generated according to a notification template you defined on the Sendbird Dashboard. The sendbird property is a JSON object which contains all the information about the message a user has sent.
As shown in the sample implementation, in step 4, you can show the parsed messages to users as a notification by using your custom ‘sendNotification()’ method.
Troubleshooting push notifications can be complex; here are some tips on doing this.
And that’s a wrap! You now know how to send push notifications for Android apps with Sendbird Chat. With the guidance in this tutorial and the docs, you’ll be on your way to sending timely, relevant, and engaging push notifications in no time!
We’e always here to help; contact us if you have any questions, comments, or concerns.
Happy push notifications building! 💻 We can’t wait to see what you build!
Stay up-to-date on the latest technical content from Sendbird.
Thank You for registering!