Get total unread message count
Query and continuously synchronize the aggregate unread message count for the current account.
Do not calculate the aggregate unread count simply by summing items in the visible pagination window; unread messages may exist in conversations not yet loaded into memory.
Code example
import sdk, {
EventOnTotalUnreadMessageCountChangedData,
GetTotalUnreadMsgCountResp,
OpenIMSDKEvent,
} from '@openimsdk/imsdk';
const response: GetTotalUnreadMsgCountResp = await sdk.getTotalUnreadMsgCount();
updateApplicationBadge(response.totalUnreadCount);
const handleUnreadChanged = (data: EventOnTotalUnreadMessageCountChangedData): void => {
updateApplicationBadge(data.totalUnreadCount);
};
const unsubscribeUnread = sdk.on(
OpenIMSDKEvent.EventOnTotalUnreadMessageCountChanged,
handleUnreadChanged,
);
export function removeTotalUnreadListener(): void {
unsubscribeUnread();
}Return value
Returns a Promise that resolves to a GetTotalUnreadMsgCountResp object:
| Property | Type | Description |
|---|---|---|
totalUnreadCount | number | The aggregate unread message count across all conversations. |
State changes after the call
Resolving the Promise yields the current baseline unread total. Listen for EventOnTotalUnreadMessageCountChanged to receive incremental updates as conversations are marked read or new messages arrive. To synchronize the app icon badge count on HarmonyOS home screens, refer to Set application badge. After reconnecting or completing background sync, re-query this method to calibrate the badge count.
Was this page helpful?