Skip to main content

Unread count

Chat clients typically need to count unread messages per channel. The Chat SDK provides APIs to:

  • Get the total unread count across all channels (excluding open channels) — getTotalUnreadCount
  • Get the unread count for a specific channel — read channel.unreadCount after calling channel.reload()
  • Get the total unread count by channel type — getUnreadCountByChannelType

Clear the unread count using clearUnreadCount when the user views a channel.

Get the total unread count

Get the total unread message count across all channel types (excluding open channels).

Method

Dart
static Future\<int\> getTotalUnreadCount(OprationHandler\<int\> handler);

Code example

Dart
int? ret = await BaseChannel.getTotalUnreadCount((int? count, NCError? error) {
if (error == null || error.isSuccess) {
print('Total unread count: $count');
}
});

Get a channel's unread count

The unread count for a specific channel is exposed as the unreadCount property on the channel object. Call channel.reload() first to fetch the latest data from the server.

Dart
final channel = DirectChannel('<target-user-id>');
await channel.reload((updatedChannel, error) {
if (error == null) {
print('Unread count: ${updatedChannel?.unreadCount}');
}
});

Get the unread count by channel type

Get the total unread count for multiple channel types (excluding open channels).

Method

Dart
static Future\<int\> getUnreadCountByChannelType(List\<ChannelType\> types, OprationHandler\<int\> handler);

Code example

Dart
final types = [ChannelType.direct, ChannelType.group];

int? ret = await BaseChannel.getUnreadCountByChannelType(types, (int? count, NCError? error) {
if (error == null || error.isSuccess) {
print('Unread count for selected types: $count');
}
});

Clear a channel's unread count

Clear the unread count for a specific channel (excluding open channels). This is a BaseChannel instance method.

Method

Dart
Future\<int\> clearUnreadCount(ErrorHandler handler);

Code example

Dart
BaseChannel channel = ...;

int? ret = await channel.clearUnreadCount((NCError? error) {
if (error == null || error.isSuccess) {
print('Unread count cleared');
}
});

Get unread @ messages

Get the list of unread @ messages in a specific channel. This is a BaseChannel instance method.

Method

Dart
Future\<int\> getUnreadMentionedMessages(OprationHandler\<List\<Message\\>> handler);

Code example

Dart
BaseChannel channel = ...;

int? ret = await channel.getUnreadMentionedMessages((List\<Message\>? messages, NCError? error) {
if (error == null || error.isSuccess) {
print('Unread @ messages: ${messages?.length}');
}
});