Business Messaging Client Side v2
Business Messaging
Version 2

How to use Chat SDK for Business Messaging

Copy link

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.


Step 1 Initialize the SDK

Copy link

First, initialize Sendbird Chat SDK. This tells the SDK which application it will communicate with as well as which features it will support.

Swift for iOSKotlin for AndroidJavaScriptFlutter
import SendbirdChatSDK

let initParams = InitParams(
    applicationId: APP_ID,
    isLocalCachingEnabled: true,
    logLevel: .info
)

SendbirdChat.initialize(params: initParams, migrationStartHandler: {
    // Migration starts.
}, completionHandler: { error in
    // Migration completed.
})

Step 2 Implement session handlers

Copy link

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)

Step 3 Authentication

Copy link

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.

Swift for iOSKotlin for AndroidJavaScriptFlutter
SendbirdChat.authenticate(userId: USER_ID, authToken: AUTH_TOKEN) { user, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

Step 4 Retrieve a feed channel

Copy link

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.

Retrieve a channel by URL

Copy link
Swift for iOSKotlin for AndroidJavaScriptFlutter
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.
}

Retrieve a list of channels

Copy link
Swift for iOSKotlin for AndroidJavaScriptFlutter
let qParams = FeedChannelListQueryParams()
qParams.includeEmptyChannel = true
qParams.limit = 10
let query = FeedChannel.createMyFeedChannelListQuery(params: qParams)

query.loadNextPage { channels, error in
    guard error == nil else {
        // Handle error.
        return
    }

    // A list of feed channels is retrieved.
}

Step 5 Create a notification collection

Copy link

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.

Swift for iOSKotlin for AndroidJavaScriptFlutter
extension CustomObject: NotificationCollectionDelegate {
    func notificationCollection(
        _ collection: NotificationCollection,
        context: NotificationContext,
        channel: FeedChannel,
        addedMessages messages: [BaseMessage]
    ) {

    }

    func notificationCollection(
        _ collection: NotificationCollection,
        context: NotificationContext,
        channel: FeedChannel,
        updatedMessages messages: [BaseMessage]
    ) {

    }

    func notificationCollection(
        _ collection: NotificationCollection,
        context: NotificationContext,
        channel: FeedChannel,
        deletedMessages messages: [BaseMessage]
    ) {

    }

    func notificationCollection(
        _ collection: NotificationCollection,
        context: FeedChannelContext,
        deletedChannel channelURL: String
    ) {

    }

    func notificationCollection(
        _ collection: NotificationCollection,
        context: FeedChannelContext,
        updatedChannel: FeedChannel
    ) {

    }

    func didDetectHugeGap(_ collection: NotificationCollection) {

    }
}

self.notificationCollection = SendbirdChat.createNotificationCollection(
    channel: channel,
    startingPoint: startingPoint,
    params: self.notificationListParams,
    delegate: customObject // The customObject is an instance of the CustomObject class.
)

self.notificationCollection?.startCollection(
    initPolicy: .cacheAndReplaceByApi,
    cacheResultHandler: { [weak self] cacheResult, error in

    }, apiResultHandler: { [weak self] apiResult, error in

    }
)

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.

Swift for iOSKotlin for AndroidJavaScriptFlutter
SendbirdChat.refreshNotificationCollections()

Step 6 Render a notification

Copy link

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:

VariableTypeDescription

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.