SDKsWASM
Track the total unread count
Get and continuously maintain the account’s total unread message count with the WASM SDK.
getTotalUnreadMsgCount() returns the current account's total unread count across all conversations at the time of the call. Do not calculate it by adding unreadCount values from only the pages currently loaded.
const { data: totalUnread } = await openimsdk.getTotalUnreadMsgCount();
updateApplicationBadge(totalUnread);Active queries establish the initial unread badge snapshot. While the application runs, listen to SdkEvent.OnTotalUnreadMessageCountChanged to keep the badge synchronized in real time:
import { SdkEvent } from '@openim/wasm-client-sdk';
const handleTotalUnreadChanged = ({ data }) => {
updateApplicationBadge(data);
};
openimsdk.on(
SdkEvent.OnTotalUnreadMessageCountChanged,
handleTotalUnreadChanged,
);
function removeTotalUnreadListener() {
openimsdk.off(
SdkEvent.OnTotalUnreadMessageCountChanged,
handleTotalUnreadChanged,
);
}Call removeTotalUnreadListener() when the component unmounts, the user signs out, or the account changes.
Was this page helpful?