Browse SDKs · React Native
Platform
SDKsReact Native

Get total unread count

Retrieve and observe the aggregate unread message counter across all conversations using the OpenIM React Native SDK.

Copy

Retrieve the aggregate unread message count across all conversations for the authenticated user. Always query this API or observe unread change events directly rather than manually summing the unreadCount of paginated conversations, which omits unread counts from unloaded conversations.

const totalUnread = await OpenIMSDK.getTotalUnreadMsgCount();

Return value

Resolves to a number representing the total unread message count.

Event listener

While the application is active in the foreground, observe unread count fluctuations via the OnTotalUnreadMessageCountChanged event:

function handleTotalUnreadChanged(totalUnread: number) {
  // Update app tab bar badge or notification icon
}

// Register listener
OpenIMSDK.on(OpenIMEvent.OnTotalUnreadMessageCountChanged, handleTotalUnreadChanged);

// Remove listener
OpenIMSDK.off(OpenIMEvent.OnTotalUnreadMessageCountChanged, handleTotalUnreadChanged);

Unregister this listener when the component unmounts, the user signs out, or accounts switch.

To synchronize this count with the OS-level badge counter, see Set application badge.