Browse SDKs · React Native
SDKsReact Native

Track the total unread count

Get the total unread message count across the current account's conversations.

Copy

Call getTotalUnreadMsgCount() to get the total unread count across all conversations for the current account. Do not calculate it by adding unreadCount from loaded pages, because unloaded conversations would be omitted.

const totalUnread = await OpenIMSDK.getTotalUnreadMsgCount();

Return result

The call returns the total unread count as a number.

Listen for changes

When the app is in the foreground, use OnTotalUnreadMessageCountChanged to listen for changes:

function handleTotalUnreadChanged(totalUnread: number) {
  // ...
}

OpenIMSDK.on(OpenIMEvent.OnTotalUnreadMessageCountChanged, handleTotalUnreadChanged);
OpenIMSDK.off(OpenIMEvent.OnTotalUnreadMessageCountChanged, handleTotalUnreadChanged);

Remove the listener when the component is destroyed, the user logs out, or the account changes.