Skip to main content

Get channels

The Chat SDK maintains a local channel list based on messages sent and received. In Swift, the primary entry point is BaseChannel, which provides shared query APIs for direct channels, group channels, and other channel types.

Get a single channel

Call BaseChannel.getChannels(identifiers:completion:) to retrieve one or more specific channels:

swift
import NexconnChatSDK

let identifier = ChannelIdentifier(channelType: .direct, channelId: "targetUserId")

BaseChannel.getChannels(identifiers: [identifier]) { channels, error in
guard let channel = channels?.first, error == nil else {
print("Failed to load channel: \(error?.localizedDescription ?? "unknown error")")
return
}

print("Channel ID: \(channel.channelId)")
print("Unread count: \(channel.unreadCount)")
print("Pinned: \(channel.isPinned)")
}

Get the channel list (paginated)

Use BaseChannel.createChannelsQuery(params:) to create a paginated local channel query:

swift
import NexconnChatSDK

let params = ChannelsQueryParams()
params.setChannelTypes([.direct, .group])
params.pageSize = 20
params.startTime = 0

let query = BaseChannel.createChannelsQuery(params: params)
query.loadNextPage { page, error in
guard let channels = page?.data, error == nil else {
print("Failed to load channels: \(error?.localizedDescription ?? "unknown error")")
return
}

for channel in channels {
print("Channel: \(channel.channelId), type: \(channel.channelType.rawValue)")
}
}

Get pinned channels

Call BaseChannel.getPinnedChannels(channelTypes:completion:) to retrieve the pinned local channel list for one or more channel types:

swift
import NexconnChatSDK

BaseChannel.getPinnedChannels(channelTypes: [NSNumber(value: ChannelType.direct.rawValue)]) { channels, error in
guard let channels, error == nil else {
print("Failed to load pinned channels: \(error?.localizedDescription ?? "unknown error")")
return
}

print("Pinned channel count: \(channels.count)")
}

Get the total unread count

Call BaseChannel.getTotalUnreadCount(completion:) to retrieve the total unread message count from the local channel store:

swift
import NexconnChatSDK

BaseChannel.getTotalUnreadCount { unreadCount, error in
if let error {
print("Failed to load unread count: \(error.localizedDescription)")
return
}

print("Total unread: \(unreadCount)")
}