Please state the version of UIKit & OS you are using when creating a topic
and select the related tags
UIKit ver 2.7.0
I want to display total unread message count using SendBird.getTotalUnreadMessageCount
(like android)
Please state the version of UIKit & OS you are using when creating a topic
and select the related tags
UIKit ver 2.7.0
I want to display total unread message count using SendBird.getTotalUnreadMessageCount
(like android)
Hi @aram,
Depending on where youâre looking to display this value, you may need to utilize the core Javascript SDK in conjunction with the UIKit to achieve your goal. Could you provide us with some context of where youâre looking to display this value?
Based on the screenshot, it sounds like you would likely need to utilize the core SDK unless youâre utilizing the Sendbird Provider HOC.
If youâre using the HOC, you could get an instance of the SDK directly from the UIKit:
const context = useSendbirdStateContext();
const sdkInstance = sendBirdSelectors.getSdk(context);
const unreadCount = sdkInstance.getTotalUnreadMessageCount();
Here is an example where I console it out: totalUnreadCount Example - CodeSandbox
Is this being called in a component that is wrapped with the SendBirdProvider
HOC?
No, but I want to implement this outside the SendBirdProvider.
Ah okay. If you want to implement outside of the provider, youâll need to utilize the Core SDK along side the UIKit. Which does mean youâd need to implement several steps such as the connection and authentication as well as the fetching of the totalUnreadCount.
itâs not working
First question, why canât I use the latest version of the library for a React version below 18? You do import from version 18 in your code for MessageInput and it doesnât work on versions below, although it is stated in your documentation
I have to use this version â@sendbird/uikit-reactâ: â3.1.0â
Second question, why itâs not working
import { FC } from 'react';
import { sendBirdSelectors, useSendbirdStateContext } from '@sendbird/uikit-react';
const UnreadMessagesHandler: FC = ({}) => {
console.log({ sendBirdSelectors });
return null;
};
export default UnreadMessagesHandler;
console:
{sendBirdSelectors: undefined} UnreadMessagesHandler.tsx:7
My component is wrapped in SendbirdProvider
<SendBirdProvider
appId={SENDBIRD_APP_ID}
colorSet={colorSet}
nickname={currentApplicationChatData.name}
theme="light"
userId={currentApplicationChatData.sendbirdUserId}
>
<UnreadMessagesHandler />
<Channel channelUrl={currentApplicationChatData.channelId} />
</SendBirdProvider>
Okay, Iâll use another import, but that doesnât work either
u haveât this method in your sdk
u example in sandbox also not working
The solution was found, I had to dig through the library code to find it
const UnreadMessagesHandler: FC<Props> = ({ channelId, setUnreadMessages, isOpened }) => {
const context = useSendbirdStateContext();
const getGroupChannel = sendbirdSelectors.getGetGroupChannel(context);
const handleUnreadMessages = useCallback(async () => {
if (!isOpened) {
const groupChannel = await getGroupChannel(channelId);
setUnreadMessages(groupChannel.unreadMessageCount);
}
}, [channelId, getGroupChannel, isOpened, setUnreadMessages]);
useEffect(() => {
handleUnreadMessages();
const getUnreadMessages = setInterval(handleUnreadMessages, 10_000); // 10s
return () => {
clearInterval(getUnreadMessages);
};
}, [handleUnreadMessages, setUnreadMessages]);
useEffect(() => {
if (isOpened) {
setUnreadMessages(0);
}
}, [isOpened, setUnreadMessages]);
return null;
};
export default UnreadMessagesHandler;
// hide by css to access to unread messages
<ChatWrapper isOpened={isOpened} ref={chatRef}>
<SendBirdProvider
appId={SENDBIRD_APP_ID}
colorSet={colorSet}
nickname={currentApplicationChatData.name}
theme="light"
userId={currentApplicationChatData.sendbirdUserId}
>
<UnreadMessagesHandler
channelId={currentApplicationChatData.channelId}
isOpened={isOpened}
setUnreadMessages={setUnreadMessages}
/>
{/* hide to prevent reading messages when chat is closed */}
{isOpened && (
<Channel channelUrl={currentApplicationChatData.channelId} />
)}
</SendBirdProvider>
</ChatWrapper>
@Tyler getTotalUnreadMessageCount()
method does not exist in sdkInstance.
Any other way the get total unread messages from all the channels that the user has?
Iâm using SendbirdProvider.
Also I have the sendbird/chat package, and when I try to get it through that the count is 0, and it should be 2:
await sendbird.connect(id, MOCKED_USER_3_ACCESS_TOKEN);
const count = await sendbird.groupChannel.getTotalUnreadMessageCount();