Manage unread message count
Retrieve and clear unread message counts across channels.
Get the total unread count across all channels
Call getTotalUnreadCount(completion:):
- Swift
- Objective-C
swift
import NexconnChatSDK
DirectChannel.getTotalUnreadCount { unreadCount, error in
print("Total unread: \(unreadCount)")
}
Objective C
[NCDirectChannel getTotalUnreadCountWithCompletion:^(NSInteger unreadCount, NCError *error) {
NSLog(@"Total unread: %ld", (long)unreadCount);
}];
Get the unread count by channel type and DND level
Call getChannelsUnreadCountByNoDisturbLevelWithParams:completion: to query unread counts filtered by channel types and do-not-disturb levels:
- Swift
- Objective-C
swift
import NexconnChatSDK
let params = ChannelsUnreadCountParams()
params.setChannelTypes([.direct, .group])
// Leave params.levels empty to include all DND levels (default + mention + muted)
// Or specify levels to filter, e.g., only count DND-off channels:
params.setLevels([.defaultLevel])
BaseChannel.getChannelsUnreadCountByNoDisturbLevel(params: params) { count, error in
print("Unread count: \(count)")
}
Objective C
NCChannelsUnreadCountParams *params = [[NCChannelsUnreadCountParams alloc] init];
params.channelTypes = @[@(NCChannelTypeDirect), @(NCChannelTypeGroup)];
// Leave params.levels empty to include all DND levels (default + mention + muted)
// Or specify levels to filter, e.g., only count DND-off channels:
params.levels = @[@(NCChannelNoDisturbLevelDefaultLevel)];
[NCBaseChannel getChannelsUnreadCountByNoDisturbLevelWithParams:params
completion:^(NSInteger count, NCError *error) {
NSLog(@"Unread count: %ld", (long)count);
}];
Get the unread count for a specific channel
The unreadCount property is available on any channel object. Reload the channel to get the latest value:
- Swift
- Objective-C
swift
import NexconnChatSDK
guard let channel = GroupChannel(channelId: "groupId") else { return }
channel.reload { updatedChannel, error in
guard let updatedChannel else { return }
print("Unread count: \(updatedChannel.unreadCount)")
}
Objective C
NCGroupChannel *channel = [[NCGroupChannel alloc] initWithChannelId:@"groupId"];
[channel reloadWithCompletion:^(NCBaseChannel *updatedChannel, NSError *error) {
NSLog(@"Unread count: %ld", (long)updatedChannel.unreadCount);
}];
Clear the unread count for a channel
Call clearUnreadCount(completion:) on a channel instance:
- Swift
- Objective-C
swift
import NexconnChatSDK
guard let channel = GroupChannel(channelId: "groupId") else { return }
channel.clearUnreadCount { isCleared, error in
if isCleared {
print("Unread count cleared.")
}
}
Objective C
[channel clearUnreadCountWithCompletion:^(BOOL isCleared, NCError *error) {
if (isCleared) {
NSLog(@"Unread count cleared.");
}
}];