Set up push notifications
To notify users of events that take place on a client app, you should set up notifications first. You can follow the steps below to set up Firebase Cloud Messaging (FCM) or Apple Push Notification service (APNs) notificatons.
Set up notification services
Step 1 Get a push token to register to the Sendbird server
You should set up FCM to receive notifications, whether you are using iOS or Android devices.
- Install Firebase messaging.
- Then, install the
- Add Firebase core.
- Initialize Firebase in the main function before running the client app.
- Then, instantiate Firebase messaging.
firebaseMessagingBackgroundHandlerto handle messages when the client app is in the background.
- You can set up local notification to receive notifications when the client app is in the foreground using the following code.
- You can get either a FCM push token if using an Android device or an APNs push token if using an iOS device and register the push token to the Sendbird server. If using an iOS device, you should grant permission to receive notifications on your device.
- You can handle notifications received using the following code.
Note: To learn more about the installation process, see Firebase Messaging Plugin for Flutter or APNs push.
Step 2 Register credentials for FCM or APNs to the Sendbird server
The FCM server requires a registration token while APNs expects an authentication token or a certificate when sending a push notification to a particular device. A user can have up to 20 device tokens for each push notification service. If the user has more than 20 device tokens, the oldest token is deleted before a new one is added. As a result, only the most recent 20 tokens are maintained.
FCM: Register a server key and a registration token on Sendbird Dashboard. To learn more, see Push notifications in our Docs for Android.
APNs: Create a SSL certificate and export a
.p12certificate or a
.p8authentication token. Then upload the file and register a device token on Sendbird Dashboard. To learn more, see Push notifications in our Docs for iOS.
Step 3 Handle a notification payload
The Sendbird server sends notification requests with a payload in
JSON format to one of the push notification services. Depending on the service you are using for push notifications, the way to handle the payload may differ. To learn more, see handle a notification payload section for FCM or APNs.
A notification message payload may consist of two properties, which are
message property is a string generated according to a notification template you set on Sendbird Dashboard. The
sendbird property is a
JSON object which contains all the information about the message a user has sent. The following is a complete payload format of the
sendbird property, and it contains a set of provided key-value items. It has the full set of information about the push notification. For example, the value of the
message key is the content of the text message received.
Note: To add to or remove from the payload the
channel_unread_countproperties, go to Settings > Chat > Push notifications on Sendbird Dashboard.