Version 1
Home
/
Notifications
/
UIKit
Notifications v1
Notifications

Install Chat UIKit for Notifications

Copy link

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.


Requirements

Copy link

The minimum requirements for Chat UIKit iOS for Notifications are:

  • iOS 11.0 and later
  • Xcode 14.1 and later
  • Swift 5.0+
  • Sendbird Chat SDK for iOS 4.6.0 and later
  • Sendbird Chat UIKit for iOS 3.5.0 and later

Before you start

Copy link

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.


Get started

Copy link

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

Copy link

To get started, open Xcode and create a new project. The UIKit supports swift.

Step 2 Install Chat UIKit for Notifications

Copy link

You can install Chat UIKit for iOS through either Swift Packages, CocoaPods, or Carthage.

Swift Packages

Copy link
  1. In Xcode, select File > Add Packages.

  2. Search for SendbirdUIKit spm repository and add it to your Package repository. You can also choose the dependency rule that you want to use in the repository and keep the latest version by selecting the main branch.

https://github.com/sendbird/sendbird-uikit-ios-spm
  1. 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 File menu, go to Packages, and select Reset Pacakge Caches. This deletes all local package data and redownloads each package from its online source.

CocoaPods

Copy link
  1. Add SendBirdUIKit into your Podfile in Xcode as below:
platform :ios, '11.0'
use_frameworks!

target YOUR_PROJECT_TARGET do
    pod 'SendBirdUIKit' # Add this line.
end
  1. Install the SendBirdUIKit framework through CocoaPods.
$ pod install
  1. Update the SendBirdUIKit framework through CocoaPods.
$ pod update

Carthage

Copy link
  1. Add SendbirdUIKit and SendbirdChatSDK into your Cartfile as below:
github "sendbird/sendbird-uikit-ios"
github "sendbird/sendbird-chat-sdk-ios"
  1. Install the SendbirdUIKit framework through Carthage.
$ carthage update --use-xcframeworks

Note: Building or creating the SendbirdUIKit framework with Carthage can only be done using the latest Swift. If your Swift is not the most recent version, the framework should be copied into your project manually.

  1. Go to your Xcode project target's General settings tab in the Frameworks and Libraries section. Then drag and drop SendbirdUIKit.xcframework and SendbirdChatSDK.xcframework from the <YOUR_XCODE_PROJECT_DIRECTORY>/Carthage/Build folder.

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

Copy link

To integrate and run Chat UIKit for Notifications in your app, you need to first initialize the SendbirdUI instance through AppDelegate.

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 SendbirdUI instance 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 completionHandler block 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.

// AppDelegate.swift

import SendbirdUIKit

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let APP_ID = "YOUR_APP_ID"    // Specify your Sendbird application ID.
    SendbirdUI.initialize(applicationId: APP_ID) { // This is the origin.
        // Initialization of SendbirdUIKit has started.
        // Show a loading indicator.
    } migrationHandler: {
        // DB migration has started.
    } completionHandler: { error in
        // If DB migration is successful, proceed to the next step.
        // If DB migration fails, an error exists.
        // Hide the loading indicator.
    }
}

Step 4 Set current user

Copy link

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 nickname and profileURL are optional, and if not specified, they'll be filled with default values.

Set 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.

// AppDelegate.swift

import SendbirdUIKit

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Initialize with APP_ID on Step 3

    // Case 1: USER_ID only
    SBUGlobals.currentUser = SBUUser(userId: {USER_ID})

    // Case 2: Specify all fields
    SBUGlobals.currentUser = SBUUser(userId: {USER_ID}, nickname:{(opt)NICKNAME}, profileURL:{(opt)PROFILE_URL})

}

Step 5 Connect to Sendbird server

Copy link

Once the currentUser is set, you can connect the user to the Sendbird server using the SendbirdUI.connect() method. 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() method also automatically updates the user profile on the Sendbird server.

When the view controller is called in Chat UIKit for Notifications, the connection is automatically made with the currentUser which should be already set in the SBUGlobals.

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:

SendbirdUI.connect { (user, error) in
    guard let user = user else {
        // The user is offline and you can't access any user information stored in the local cache.
        return
    }
    if let error = error {
        // The user is offline but you can access user information stored in the local cache.
    }
    else {
        // The user is online and connected to the server.
    }
}

Step 6 Register push notification credentials

Copy link

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 7 Display notification channel

Copy link

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.

Feed view

Copy link

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.

let channelVC = SBUViewControllerSet.FeedNotificationChannelViewController.init(
    channelURL: "FEED_CHANNEL_CHANNEL_URL"
)
self.navigationController?.pushViewController(channelVC, animated: true)

Channel list view

Copy link

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.

let channelListVC = SBUViewControllerSet.GroupChannelListViewController.init()
let naviVC = UINavigationController(rootViewController: channelListVC)
viewController?.present(naviVC, animated: true)