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);The query itself does not trigger an event. This page provides the complete SdkEvent.OnTotalUnreadMessageCountChanged listener for keeping the total unread count current while the application is in the foreground:
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?