Retrieve channels
The Chat SDK generates and maintains a local channel list in the database based on sent and received messages. You can retrieve channels from this local store. Community channels can also be included in the list if the feature is enabled for your app. Submit a ticket to enable it.
Get one or more channels
Call BaseChannel.getChannels(identifiers:completion:) to retrieve channel details by identifier.
- Swift
- Objective-C
import NexconnChatSDK
let id1 = ChannelIdentifier(channelType: .direct, channelId: "userId")
let id2 = ChannelIdentifier(channelType: .group, channelId: "groupId")
BaseChannel.getChannels(identifiers: [id1, id2]) { channels, error in
guard let channels, error == nil else {
print("Error fetching channels: \(error?.localizedDescription ?? "unknown error")")
return
}
print("Fetched channels: \(channels.count)")
}
NCChannelIdentifier *id1 = [[NCChannelIdentifier alloc] initWithChannelType:NCChannelTypeDirect
channelId:@"userId"];
NCChannelIdentifier *id2 = [[NCChannelIdentifier alloc] initWithChannelType:NCChannelTypeGroup
channelId:@"groupId"];
[NCBaseChannel getChannels:@[id1, id2] completion:^(NSArray<NCBaseChannel *> * _Nullable channels, NCError * _Nullable error) {
if (!error) {
// Handle channels
} else {
NSLog(@"Error fetching channels: %@", error);
}
}];
| Parameter | Type | Description |
|---|---|---|
| identifiers | NSArray<NCChannelIdentifier *> | Channel identifiers to query |
| completion | Block | Result callback; returns the channel array on success, or NCError on failure |
Sync remote channels to local
Submit a ticket to enable this feature.
When a user reinstalls the app or logs in on a new device, call BaseChannel.syncRemoteChannels(completion:) to sync up to 1,000 channels from the server to the local database.
This method only triggers the sync request. Listen for onRemoteChannelsSyncCompleted(_:) on ChannelHandler to know when the sync finishes.
- Swift
- Objective-C
import NexconnChatSDK
BaseChannel.syncRemoteChannels { error in
if error == nil {
print("Sync request sent")
}
}
[NCBaseChannel syncRemoteChannelsWithCompletion:^(NCError * _Nullable error) {
if (!error) {
NSLog(@"Sync request sent");
} else {
NSLog(@"Sync request failed: %@", error);
}
}];
- Swift
- Objective-C
import NexconnChatSDK
final class ChannelSyncService: NSObject, ChannelHandler {
func start() {
NCEngine.addChannelHandler(identifier: "MyHandler", handler: self)
}
func onRemoteChannelsSyncCompleted(_ event: RemoteChannelsSyncCompletedEvent) {
if event.error == nil {
print("Channel sync complete")
} else {
print("Channel sync failed: \(event.error!.localizedDescription)")
}
}
}
[NCEngine addChannelHandlerWithIdentifier:@"MyHandler" handler:self];
- (void)onRemoteChannelsSyncCompleted:(NCRemoteChannelsSyncCompletedEvent *)event {
if (event.error == nil) {
NSLog(@"Channel sync complete");
} else {
NSLog(@"Channel sync failed: %@", event.error);
}
}
Get channel list (paginated)
Use BaseChannel.createChannelsQuery(params:) to retrieve the local channel list page by page. Results are sorted by time in descending order, and pinned channels are prioritized by default.
Pass 0 as the initial startTime. For later pages, the SDK advances the cursor internally after each loadNextPage(_:) call.
- Swift
- Objective-C
import NexconnChatSDK
let params = ChannelsQueryParams()
params.setChannelTypes([.direct, .group])
params.pageSize = 100
params.startTime = 0
params.topPriority = true
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
}
print("Loaded channels: \(channels.count)")
}
The current public iOS SDK documents paginated loadNextPage(_:) channel queries in Swift. Verify the generated framework header in your build if you need the Objective-C selector surface for this query type.
| Parameter | Type | Description |
|---|---|---|
| channelTypes | NSArray | Array of channel types. Required. |
| pageSize | int32 | Number of channels to retrieve per page. Default is 20. |
| startTime | long long | Timestamp cursor. Pass 0 for the first page. |
| topPriority | BOOL | Whether to prioritize pinned channels. Default is YES. |
Get unread channel list
Call BaseChannel.getUnreadChannels(_:completion:) to get channels with unread messages. This API supports direct, group, and system channels.
- Swift
- Objective-C
import NexconnChatSDK
BaseChannel.getUnreadChannels(
channelTypes: [
NSNumber(value: ChannelType.group.rawValue),
NSNumber(value: ChannelType.system.rawValue),
NSNumber(value: ChannelType.direct.rawValue),
]
) { channels, error in
guard let channels, error == nil else {
return
}
print("Unread channels: \(channels.count)")
}
[NCBaseChannel getUnreadChannels:@[@(NCChannelTypeGroup), @(NCChannelTypeSystem), @(NCChannelTypeDirect)]
completion:^(NSArray<NCBaseChannel *> * _Nullable channels, NCError * _Nullable error) {
if (!error) {
// Handle channels with unread messages
}
}];
Get channels with unread mentions
Use BaseChannel.createUnreadMentionMeChannelsQuery(params:) to get channels where the current user has unread mentions. This API supports direct, group, system, and community channels.
- Swift
- Objective-C
import NexconnChatSDK
let params = UnreadMentionMeChannelsQueryParams()
params.setChannelTypes([.direct, .group, .community])
params.pageSize = 100
params.startTime = 0
let query = BaseChannel.createUnreadMentionMeChannelsQuery(params: params)
query.loadNextPage { page, error in
guard let channels = page?.data, error == nil else {
return
}
print("Unread mention channels: \(channels.count)")
}
The current public iOS SDK documents paginated loadNextPage(_:) unread-mention queries in Swift. Verify the generated framework header in your build if you need the Objective-C selector surface for this query type.
| Parameter | Type | Description |
|---|---|---|
| channelTypes | NSArray | Channel type array. Required. |
| topPriority | BOOL | Whether to prioritize pinned channels. Default is YES. |
| startTime | long long | Start time for the query. Pass 0 for the latest page. |
| pageSize | NSUInteger | Number of results per page. Default is 20. |
Handling reinstalls or device switches
If a user reinstalls the app or logs in on a new device, the channel list may appear empty or incomplete because:
- Uninstalling deletes the local database, so there are no local messages to generate channels from.
- A new device may not yet have local message history.
- If multi-device message sync is enabled, the server syncs only recent messages, which can result in a partial channel list.
To recover the previous channel list:
- Request an extension of the missed-message compensation period, up to 7 days. Submit a ticket if you need this.
- Maintain the channel list on your app server and use server-side history APIs when you need a complete historical list.