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 unread count filtered by DND levels — getChannelsUnreadCountByNoDisturbLevel

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

note

Use the callback to read the final result. On this page, ErrorHandler callbacks succeed when error?.isSuccess == true, while OperationHandler callbacks succeed when error == null.

Get the total unread count

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

Method

Dart
static Future<int> getTotalUnreadCount(OperationHandler<int> handler);

Code example

Dart
int? ret = await BaseChannel.getTotalUnreadCount((int? count, NCError? error) {
if (error == null) {
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}');
}
});

Unread count by channel type

The current Flutter wrapper does not expose a dedicated helper that returns a single aggregated unread total for an arbitrary list of channel types.

Use one of these supported alternatives instead:

  • BaseChannel.getUnreadChannels() and sum each channel's unreadCount
  • BaseChannel.getChannelsUnreadCountByNoDisturbLevel() when your filter is based on channel type plus DND level

Get the unread count by DND level

Get the total unread count filtered by channel type and DND level.

Method

Dart
static Future<int> getChannelsUnreadCountByNoDisturbLevel(ChannelsUnreadCountParams params, OperationHandler<int> handler)

Code example

Dart
await BaseChannel.getChannelsUnreadCountByNoDisturbLevel(
ChannelsUnreadCountParams(
channelTypes: [ChannelType.direct, ChannelType.group],
levels: [ChannelNoDisturbLevel.allMessage, ChannelNoDisturbLevel.mention],
),
(count, error) {
if (error == null) {
print('Unread count: $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?.isSuccess == true) {
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(OperationHandler<List<Message>> handler);

Code example

Dart
BaseChannel channel = ...;

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

Get the first unread message

Get the first unread message in a specific channel. This is a BaseChannel instance method.

Method

Dart
Future<int> getFirstUnreadMessage(OperationHandler<Message> handler)

Code example

Dart
BaseChannel channel = ...;

await channel.getFirstUnreadMessage((message, error) {
if (error == null) {
print('First unread message: ${message?.messageId}');
}
});