Set up push notifications
To notify users of events that take place on a client app, you should first set up notifications. The following steps in this page are written on the assumption that you have already registered the certificates and credentials of FCM and APNs to the Sendbird server via your dashboard or through Platform API requests.
Note: Push notifications are only available in group channels.
Step 1: Install libraries and configure FCM and APNs
@react-native-firebase/app library is an easy-to-use FCM library for simple integration. You can configure FCM for your React Native project by following the instructions in the Notifications page from the React Native Firebase documentation.
Note: Because the Google Cloud Messaging (GCM) server and client APIs are deprecated and removed as of April 11, 2019, we recommend that you use FCM and develop push notifications with the
@react-native-firebase/applibrary for your project.
To receive push notifications on iOS devices, you can integrate APNs by installing the
@react-native-community/push-notification-ios library. For more details, see the Install section of the library's documentation.
Step 2: Register tokens for FCM and APNs to the Sendbird server
Note: A user can have up to 20 FCM registration tokens and 20 APNs device tokens each. The oldest token will be deleted before a new token is added for a user who already has 20 registration or device tokens. Only the 20 newest tokens will be maintained for users who already have more than 20 of each token type.
The FCM server requires an FCM registration token and an APNs device token for client app instances when targeting a particular device to send a push notification. The Chat SDK provides an interface to register and unregister two types of tokens to the Sendbird server. The tokens are used to communicate with the FCM server. The following is an example of the registration in our React Native sample.
Step 3: Receive notification messages
Once the token is registered, Android client apps receive notification messages via FCM, and iOS client apps via APNs.
While APNs requires no further configuration in receiving these messages, FCM requires the client app instance to receive and handle FCM notification messages. Instructions on the Receiving Messages section show you how to receive FCM messages based on the state (foreground, background, or quit) of the client app.
The sample code below shows how to receive and handle FCM messages while the client app is in the background or quit state and display them using local notifications.
Note: For Android 8.0 (API level 26) and higher, all notifications must be assigned to a notification channel, so you should provide a
Notificationinstance with the required data such as the ID of a notification channel.
The sample code below shows how to handle local notifications in Android and iOS. Refer to the event sections of the Notifee and push-notification-ios libraries to learn more.
data object above, which is parsed from an FCM data message, contains a set of key-value items shown in the below
JSON format. The
data object has a full set of information about the push notification. For example, the value of the
message key means a received text message. As for the
channel_unread_count, the attribute can be added into or removed from the payload in Settings > Application > Notifications on Sendbird Dashboard.