Install Chat UIKit for Notifications
Sendbird Chat UIKit for Notifications is a set of prebuilt UI components that allows you to easily craft an in-app notification channel. Our development kit includes light and dark themes, fonts, colors and more. You can install Chat UIKit for Notifications on the client app to receive notifications sent through the Notifications API or an external platform. Follow the steps below.
Note: Sendbird Notifications uses the existing Chat UIKit for iOS v11.0 and later. To learn more about Sendbird UIKit for Chat, refer to this page.
The minimum requirements for Chat UIKit iOS for Notifications are:
iOS 11.0 and later
Xcode 14.1 and later
Sendbird Chat SDK for iOS 4.6.0 and later
Sendbird Chat UIKit for iOS 3.5.0 and later
Before you start
Before installing Sendbird Chat SDK, you need to create a Sendbird application on the Sendbird Dashboard, which comprises everything required in a chat and notification service including users, notifications, and channels. You will need the Application ID of your Sendbird application when initializing the SDK.
Note: Each Sendbird application can only be integrated with a single client app.
You can start building a notification channel in your app by installing Chat UIKit for Notifications. This developer kit is an add-on feature to Sendbird Chat SDK so installing it will also install the core Chat SDK.
Step 1 Create a project
To get started, open
Xcode and create a new project. The UIKit supports
Step 2 Install Chat UIKit for Notifications
You can install Chat UIKit for iOS through either Swift Packages, CocoaPods, or Carthage.
In Xcode, select File > Add Packages.
SendbirdUIKit spm repositoryand add it to your
Package repository. You can also choose the
dependency rulethat you want to use in the repository and keep the latest version by selecting the
- When finished, Xcode automatically begins resolving and downloading your dependencies to the repository in the background.
Note: A build error may occur whlie using Swift packages with Xcode due to issues with caching. To resolve this error, try resetting the Xcode package caches. Open the
Filemenu, go to
Packages, and select
Reset Pacakge Caches. This deletes all local package data and redownloads each package from its online source.
Podfilein Xcode as below:
- Install the
- Update the
- Install the
Note: Building or creating the
Carthagecan only be done using the latest
Swift. If your
Swiftis not the most recent version, the framework should be copied into your project manually.
- Go to your Xcode project target's General settings tab in the
Frameworks and Librariessection. Then drag and drop
Note: Errors may occur if you're building your project with Xcode 11.3 or earlier versions. To fix these errors, refer to Handle errors caused by unknown attributes.
Step 3 Initialize with APP_ID
To integrate and run Chat UIKit for Notifications in your app, you need to first initialize the
SendbirdUI instance through
Note: Chat UIKit for Notifications uses local caching so that the client app can locally cache and retrieve channel and message data. The initialization process of the
SendbirdUIinstance is asynchronous and requires you to receive a callback function before you can move onto the next step. If the database fails to migrate, the
completionHandlerblock is called with an error. If the database successfully migrates, there's no error and you can proceed to the next step. Refer to the updated code below.
Until the initialization of Chat UIKit for Notifications is completed, the progress doesn't advance to the next step. We recommend displaying a loading indicator to show the initialization process so that other succeeding steps should wait. The indicator should be displayed when
startHandler is called and hidden when
completionHandler is called.
Step 4 Set current user
User information must be set as
currentUser in the
SBUGlobals prior to launching Chat UIKit for Notifications. This information will be used for various tasks within the kit, and if you don't set
currentUser in advance, there will be restrictions while using the UIKit. The
userID field shown below must be specified. Other fields such as
profileURL are optional, and if not specified, they'll be filled with default values.
currentUser for the UIKit through the
AppDelegate as below:
Note: Even if you don't use
AppDelegate, you should still register user information before launching a chat service.
Step 5 Set an access token
Sendbird server authenticates requests from client apps through two methods: using only user ID or using a user ID and the user's access token.
The user ID authentication method can be useful for applications in development or for existing services where users can log in as guests without additional security. Meanwhile, the access token authentication method generates access tokens for each user through Chat API. Once you request and obtain a token, you need to use that specific access token to authenticate the user.
Step 6 Connect or authenticate to Sendbird server
There are two methods to connect to the Sendbird server:
SendbirdUI.authenticateFeed(completionHandler:). If you wish to only implement feed view in your app, it's highly recommended that you authenticate a user to the Sendbird server. If you wish to display chat view or if you're already using Chat SDK and want to additionally implement feed view, then you should connect a user to the Sendbird server.
Connect a user
currentUser is set, you can connect the user to the Sendbird server using
SendbirdUI.connect(completionHandler:). You need to call this method to connect to the Sendbird server if you wish to only use chat view or use both chat and feed views in your app. The
SendbirdUI.connect(completionHandler:) method creates an active WebSocket connection with the server, which allows the Chat SDK to receive real-time events.
If the user ID used during the connection attempt doesn't exist on the Sendbird server, a new user account is created with the specified user ID. The
SendbirdUI.connect(completionHandler:) method also automatically updates the user profile on the Sendbird server.
When the view controller is called in UIKit for Notifications, the connection is automatically made with the
currentUser which should be already set in the
With local caching added to Sendbird Chat SDK, the latest user instance may be returned through the callback even when the user is offline. The local caching functionality stores message and channel data in the local storage and the Sendbird server. As a result, even when a user isn't connected to the server, the user information stored in the local cache is returned through the callback along with an error indicating the offline status.
Refer to the code below to see how to connect a user to the Sendbird server:
Authenticate a user
currentUser is set, you can authenticate the user to the Sendbird server using
SendbirdUI.authenticateFeed(completionHandler:). It's highly recommended to use this method to authenticate to the Sendbird server if you're only displaying feed view in the client app.
When keeping your notification channels up to date, you can either create an active WebSocket connection with the server or manually refresh the channel with latest information. In this case where you're displaying a feed view by authenticating a user to the server, you need to refresh notification collections to update your channel. This is because the Chat SDK can't receive real-time events to notify the user with updated information about the feed view when there's no active WebSocket connection with the server.
In order to use the
SendbirdUI.authenticateFeed(completionHandler:) method, you must first issue a session token. After authenticating with the session token, you must set a session delegate so that the client app can renew the token upon the user's session expiration. When using
SendbirdUI.authenticateFeed(completionHandler:), all session keys are set to expire so it's important that you implement the session handler in order to keep using Sendbird Notifications.
Then, you can call
SendbirdUI.authenticateFeed(completionHandler:) to create a new user account or log in with an existing account with the specified user ID and session token. This method authenticates the UIKit and allows it to retrieve feed channels, display notification messages, and more. Refer to the code snippet below.
Note: If you use the
authenticateFeed(completionHandler:)method to connect to the Sendbird server, you can't access group channels or open channels. Error messages, such as
User auth session is not allowed to use requested service., will be returned when trying to use Chat SDK's public interfaces.
Step 7 Register push notification credentials
In order to receive push notifications, you need to register notification credentials first. Refer to this page on how to upload your push certificate and register a device token.
Step 8 Display notification channel
Refer to the codes below to display either Feed view or channel list view where you can see the notifications that were sent to the channel.
In order to display the Feed view of your notification channel, you need to first find its channel URL on Sendbird Dashboard under Notifications > Channels.
Implement the following code to create a view controller.
Channel list view
To see the Chat view of your notification channel, you need to first display and call the channel list view in the UIKit. The channel list contains a list of all group channels that the user is a member of along with the notification channel.
Implement the following code to create a view controller.