This guide outlines the steps to integrate Sendbird Chat SDK for Sendbird Business Messaging, from initialization to displaying notifications in a channel. Follow the instructions below to implement essential functionalities for Business Messaging. Sendbird Business Messaging is currently supported by Chat SDK on these platforms:
Chat SDK for iOS
Chat SDK for Android
Chat SDK for JavaScript
Chat SDK for Flutter
Note: Before we dive into the client side implementation of Sendbird Business Messaging, it is assumed you have already installed the Chat SDK. If not, install the Chat SDK by following this guide.
In order to ensure that a user's notifications are properly secured, Sendbird strongly recommends that our customers utilize sessionTokens in order for users to connect via the SDK. For this reason, you will need to implement session handlers in order to handle expiring or revoked sessionTokens.
For data security, Sendbird strongly recommends our customers do not directly call the Platform APIs from a client-facing application. Due to this reason, you should implement a service to call your backend to refresh the users token.
Note: Session handlers must be set prior to authentication in order for them to function correctly.
Swift for iOSKotlin for AndroidJavaScriptFlutter
extension CustomObject: SessionDelegate {
func sessionTokenDidRequire(successCompletion success: @escaping (String?) -> Void, failCompletion fail: @escaping () -> Void) {
// A new session token is required in the SDK to refresh the session.
// Refresh the session token and pass it onto the SDK through success(NEW_TOKEN).
// If you don't want to refresh the session, pass on a nil value through success(nil).
// If any error occurs while refreshing the token, let the SDK know about it through fail().
}
func sessionWasClosed() {
// The session refresh has been denied from the app.
// The client app should guide the user to a login page to log in again.
}
func sessionWasRefreshed() {
// Optional. No action is required.
// This is called when the session is refreshed.
}
func sessionDidHaveError(_ error: SBError) {
// Optional. No action is required.
// This is called when an error occurs during the session refresh.
}
}
SendbirdChat.setSessionDelegate(customObject)
Now that we've initialized our application and registered our session handlers, we can now authenticate to the Sendbird servers through the authenticate method on the SDK.
In order to fetch a list of notifications, first fetch the channel that the notifications belong to. We can achieve this in one of two ways. If we know the channel_url of the channel we want to load, we can fetch the channel directly. Otherwise, we can fetch a list of channels first.
FeedChannel.getChannel(url: CHANNEL_URL) { channel, error in
guard error == nil else {
// Handle error.
return
}
// Through the channel parameter of the callback method,
// the feed channel object identified with CHANNEL_URL is returned by the Sendbird server,
// and you can get the feed channel's data from the result object.
}
Once a feed channel is retrieved, you can create a NotificationCollection. The collection is an instance that allows you to swiftly create a Notification Channel view that contains all of the necessary data.
In order to keep your collection up to date, you should utilize the refreshNotificationCollections to check for new notifications or channel updates. It's recommended that you refresh the collection whenever the application goes from the background to the foreground.
The final step is to render a Notification. Each Notification will contain a notificationData object that contains all the variable information about a single notification. notificationData contains the following information:
Variable
Type
Description
label
string
The sub-category of the notification. You can utilize this as a filter on the UI.
templateKey
string
The unique key to a template. You can set the key when creating a template on the dashboard.
tags
Array of strings
The tags associated with the template. To learn how to use tags, see the Tags guide.
templateVariables
Object of key-value pairs
The key-value pairs of the variables you've set to the template. You can edit the variables through Template editor on the dashboard.