Authentication
In order to use the features of Sendbird Chat SwiftUI in your client apps, a SendbirdUI
instance must be initiated in each client app through user authentication with the Sendbird server. The instance communicates and interacts with the server using an authenticated user account, and is allowed to use the SwiftUI's features. This page explains how to authenticate your user with the server.
Set an access token
The Sendbird server authenticates requests from client apps through two methods: One is using only user ID, the other is using a user ID along with the user's access token.
The user ID authentication can be useful for applications in development or existing services where additional security is not necessarily needed. On the other hand, the access token authentication generates access tokens for each user through Chat Platform API. Once you request and obtain a token for a user, you need to use that specific access token to authenticate the user.
Connect to Sendbird server
The SendbirdUI.connect()
method connects a user to the Sendbird server using the preset information of the currentUser
. If the user ID used during the connection attempt doesn't exist on our 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 Sendbird View
is called in SwiftUI, 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 Sendbird server. As a result, even when a user is not 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 Sendbird server:
Disconnect from Sendbird server
When a user doesn’t need to receive messages from the Sendbird server, it is recommended to disconnect the user from the server. Even after disconnected, the user can still receive push notifications for new messages delivered by APNs (Apple Push Notification Service).
Register for push notifications
Push notifications are a type of notification sent to your user's device when a client app is running in the background. Sendbird server will communicate with APNs whenever needed and APNs will send a push notification to the client app on iOS devices. Push notifications for the client app will contain a payload created by Sendbird and be delivered through APNs. In order to use this feature, you need to register the user's device token to Sendbird server through the AppDelegate
.
Note : In the SwiftUI project, we set up APNs by adding an
AppDelegate
.
Note : You can use these methods when the
SBUGlobals.currentUser
is set and connected to Sendbird server.
Handle push notification event
Sendbird Chat SwiftUI includes a PushPayloadManager
that handles secondary events on behalf of the user when a push notification is received. The PushPayloadManager
processes the logic for navigating to the channel with the received message in the channel list after receiving a push notification or emits the push event through its Combine publisher, pushNotificationPublisher
.
To deliver a push event to the PushPayloadManager, use the save
method in the UNUserNotificationCenterDelegate
to pass the userInfo
information.
Users can subscribe to Sendbird's push events through the pushNotificationPublisher
provided by the PushPayloadManager
. In a SwiftUI view, it can be used as follows:
Note : If a view that subscribes to a publisher using
onReceive
is not present in the SwiftUI view hierarchy, it will not receive events. To continuously observe events, it is recommended to subscribe to events from the app’s root view.
Unregister for push notifications
You should unregister a user's device token from Sendbird server if you don’t want to send push notifications to the user.
Note : You can use these methods when the
SBUGlobals.currentUser
is set and connected to Sendbird server.
Update user profile
When a currentUser
is created with a unique ID, Sendbird Chat SwiftUI automatically generates nickname
and profileImage
of the user based on the specified userId
. You can also modify these properties by yourself when needed.
Note : You can use this method when the
SBUGlobals.currentUser
is set and connected to Sendbird server.