V2TIMConversation
information of each conversation and add the unreadCount
values of all the V2TIMConversation
objects to get the final result and display it on the UI.
The Chat SDK provides the getTotalUnreadMessageCount
API to query the total unread count of all the conversations.
When the total unread count changes, the SDK will notify the latest total unread count through the onTotalUnreadMessageCountChanged
callback.getTotalUnreadMessageCount
(Android / iOS and macOS / Windows) to get the total unread message count of all conversations and display it on the UI.V2TIMManager.getConversationManager().getTotalUnreadMessageCount(new V2TIMValueCallback<Long>() {@Overridepublic void onSuccess(Long aLong) {Log.i("imsdk", "success");}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
[[V2TIMManager sharedInstance] getTotalUnreadMessageCount:^(UInt64 totalCount) {// Obtained successfully. `totalCount` is the total unread message count of all conversations.// Update the unread count on the UI} fail:^(int code, NSString *desc) {// Failed to obtain}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};auto callback = new ValueCallback<uint64_t>{};callback->SetCallback([=](const uint64_t& count) {// Got the total unread count of all conversations successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to get the total unread count of all conversationsdelete callback;});V2TIMManager::GetInstance()->GetConversationManager()->GetTotalUnreadMessageCount(callback);
addConversationListener
(Android / iOS and Mac / Windows) to add a conversation listener to receive notifications of a change in the total unread count of all conversations (such notifications can be received only after the getTotalUnreadMessageCount
API is called).onTotalUnreadMessageCountChanged
(Android / iOS and macOS / Windows) of V2TIMConversationListener
.public void onTotalUnreadMessageCountChanged(long totalUnreadCount) {// Received a notification of a change in the total unread count of all conversationsLog.i("imsdk", "onTotalUnreadMessageCountChanged");}
// Add a conversation listener[[V2TIMManager sharedInstance] addConversationListener:self];// Received a notification of a change in the total unread count of all conversations- (void)onTotalUnreadMessageCountChanged:(UInt64)totalUnreadCount {// `totalUnreadCount` is the total unread count.}
class ConversationListener final : public V2TIMConversationListener {public:/*** Notification of the change of the total unread message count of all conversations (supported by 5.3.425 or later)** @note* - The total unread message count excludes the unread message count of Do-Not-Disturb conversations (conversations whose message receiving option is* V2TIM_NOT_RECEIVE_MESSAGE or V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE).*/void OnTotalUnreadMessageCountChanged(uint64_t totalUnreadCount) override {// Received a notification of a change in the total unread count of all conversations}// Other member functions...};// Add a conversation event listener. Keep `conversationListener` valid before the listener is removed to ensure event callbacks are received.ConversationListener conversationListener;V2TIMManager::GetInstance()->GetConversationManager()->AddConversationListener(&conversationListener);
getUnreadMessageCountByFilter
API for you to query the total unread message count of some conversations based on filters.
To listen for the changes of the total unread message count of some conversations based on filters, call subscribeUnreadMessageCountByFilter
to register the listening. When the total unread count under the specified filters changes, the SDK will notify you of the latest total unread count through the onUnreadMessageCountChangedByFilter
callback.getUnreadMessageCountByFilter
(Android / iOS and Mac / Windows) to get the total unread message count of some conversations and update it on the UI. The filter condition V2TIMConversationListFilter
is described as follows:Attribute | Definition | Description |
type | Conversation type ( 0 indicates that this item is not filtered.) | One-to-one or group conversation |
conversationGroup | Conversation group name (If no value is entered, this item is not filtered.) | It is not the group name but the conversation group name. For more information, see Conversation Group. |
markType | Conversation tag type ( 0 indicates that this item is not filtered.) |
V2TIMConversationListFilter filter = new V2TIMConversationListFilter();filter.setConversationType(V2TIMConversation.V2TIM_GROUP);filter.setConversationGroup("conversation_group");filter.setMarkType(V2TIMConversation.V2TIM_CONVERSATION_MARK_TYPE_STAR);V2TIMManager.getConversationManager().getUnreadMessageCountByFilter(filter, new V2TIMValueCallback<Long>() {@Overridepublic void onSuccess(Long totalUnreadCount) {tvLog.setText("getUnreadMessageCountByFilter success, totalUnreadCount:" + totalUnreadCount);}@Overridepublic void onError(int code, String desc) {tvLog.setText("getUnreadMessageCountByFilter failed");}});
V2TIMConversationListFilter *filter = [[V2TIMConversationListFilter alloc] init];filter.type = V2TIM_GROUP;filter.conversationGroup = @"conversation_group";filter.markType = V2TIM_CONVERSATION_MARK_TYPE_STAR;[[V2TIMManager sharedInstance] getUnreadMessageCountByFilter:filter succ:^(UInt64 totalUnreadCount) {[self appendString:[NSString stringWithFormat:@"getUnreadMessageCountByFilter success totalUnreadCount:%llu", totalUnreadCount]];} fail:^(int code, NSString *desc) {[self appendString:[NSString stringWithFormat:@"getUnreadMessageCountByFilter failed"]];}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};auto callback = new ValueCallback<uint64_t>{};callback->SetCallback([=](const uint64_t& count) {// Got the total unread count successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to get the total unread countdelete callback;});V2TIMConversationListFilter filter;filter.type = V2TIM_GROUP;filter.conversationGroup = "conversation_group";filter.markType = V2TIM_CONVERSATION_MARK_TYPE_STAR;V2TIMManager::GetInstance()->GetConversationManager()->GetUnreadMessageCountByFilter(filter, callback);
addConversationListener
(Android / iOS and macOS / Windows) to add a conversation listener. Then call the subscribeUnreadMessageCountByFilter
(Android / iOS and macOS / Windows) API to register the listening for the changes of the total unread message count under specified filters.onUnreadMessageCountChangedByFilter
(Android / iOS and macOS / Windows) of V2TIMConversationListener
.filter
parameter in the onUnreadMessageCountChangedByFilter
callback lists the filters specified when subscribeUnreadMessageCountByFilter
is called. The filter
parameter contains the conversationType
, conversationGroup
, and markType
fields. We can distinguish between different filters by checking whether all three fields are the same.// Add a conversation listenerV2TIMManager.getConversationManager().addConversationListener(conversationListener);// Register the listening for the changes of the total unread message count under specified filtersV2TIMConversationListFilter filter = new V2TIMConversationListFilter();filter.setConversationType(V2TIMConversation.V2TIM_GROUP);filter.setConversationGroup("conversation_group");filter.setMarkType(V2TIMConversation.V2TIM_CONVERSATION_MARK_TYPE_STAR);V2TIMManager.getConversationManager().subscribeUnreadMessageCountByFilter(filter);// Notification on the change of the total unread message count under specified filterspublic void onUnreadMessageCountChangedByFilter(V2TIMConversationListFilter filter, long totalUnreadCount) {// `filter` indicates the filter conditions. `totalUnreadCount` indicates the total unread message count.Log.i(TAG, "onUnreadMessageCountChangedByFilter:" + totalUnreadCount + "\\n");}
// Add a conversation listener[[V2TIMManager sharedInstance] addConversationListener:self];// Register the listening for the changes of the total unread message count under specified filtersV2TIMConversationListFilter *filter = [[V2TIMConversationListFilter alloc] init];filter.type = V2TIM_GROUP;filter.conversationGroup = @"conversation_group";filter.markType = V2TIM_CONVERSATION_MARK_TYPE_STAR;[[V2TIMManager sharedInstance] subscribeUnreadMessageCountByFilter:filter];// Notification on the change of the total unread message count under specified filters- (void)onUnreadMessageCountChangedByFilter:(V2TIMConversationListFilter *)filter totalUnreadCount:(UInt64)totalUnreadCount {// `filter` indicates the filter conditions. `totalUnreadCount` indicates the total unread message count.}
class ConversationListener final : public V2TIMConversationListener {public:// Notification on the change of the total unread message count under specified filtersvoid OnUnreadMessageCountChangedByFilter(const V2TIMConversationListFilter &filter, uint64_t totalUnreadCount) override {// `filter` indicates the filter conditions. `totalUnreadCount` indicates the total unread message count.}// Other member functions...};// Add a conversation event listener. Keep `conversationListener` valid before the listener is removed to ensure event callbacks are received.ConversationListener conversationListener;V2TIMManager::GetInstance()->GetConversationManager()->AddConversationListener(&conversationListener);// Register the listening for the changes of the total unread message count under specified filtersV2TIMConversationListFilter filter;filter.type = V2TIM_GROUP;filter.conversationGroup = "conversation_group";filter.markType = V2TIM_CONVERSATION_MARK_TYPE_STAR;V2TIMManager::GetInstance()->GetConversationManager()->SubscribeUnreadMessageCountByFilter(filter);
unsubscribeUnreadMessageCountByFilter
(Android / iOS and macOS / Windows) API to cancel the listening for the changes of the total unread message count under specified filters.// Cancel the listening for the changes of the total unread message count under specified filtersV2TIMConversationListFilter filter = new V2TIMConversationListFilter();filter.setConversationType(V2TIMConversation.V2TIM_GROUP);filter.setConversationGroup("conversation_group");filter.setMarkType(V2TIMConversation.V2TIM_CONVERSATION_MARK_TYPE_STAR);V2TIMManager.getConversationManager().unsubscribeUnreadMessageCountByFilter(filter);
// Cancel the listening for the changes of the total unread message count under specified filtersV2TIMConversationListFilter *filter = [[V2TIMConversationListFilter alloc] init];filter.type = V2TIM_GROUP;filter.conversationGroup = @"conversation_group";filter.markType = V2TIM_CONVERSATION_MARK_TYPE_STAR;[[V2TIMManager sharedInstance] unsubscribeUnreadMessageCountByFilter:filter];
// Cancel the listening for the changes of the total unread message count under specified filtersV2TIMConversationListFilter filter;filter.type = V2TIM_GROUP;filter.conversationGroup = "conversation_group";filter.markType = V2TIM_CONVERSATION_MARK_TYPE_STAR;V2TIMManager::GetInstance()->GetConversationManager()->UnsubscribeUnreadMessageCountByFilter(filter);
markC2CMessageAsRead
is used to clear the unread count of a one-to-one conversation.markGroupMessageAsRead
is used to clear the unread count of a group conversation.markAllMessageAsRead
is used to clear the unread count of all the conversations.markC2CMessageAsRead
(Android / iOS and macOS / Windows) to clear the unread count of a specified one-to-one conversation.String userID = "userID";V2TIMManager.getMessageManager().markC2CMessageAsRead(userID, new V2TIMCallback() {@Overridepublic void onSuccess() {Log.i("imsdk", "success");}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
[[V2TIMManager sharedInstance] markC2CMessageAsRead:@"conversationID" // ID of the target one-to-one conversationsucc:^{// Cleared the unread count successfully} fail:^(int code, NSString *msg) {// Failed to clear the unread count}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString userID = u8"userID";auto callback = new Callback;callback->SetCallback([=]() {// Cleared the unread count successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to clear the unread countdelete callback;});V2TIMManager::GetInstance()->GetMessageManager()->MarkC2CMessageAsRead(userID, callback);
markC2CMessageAsRead
is called successfully:addConversationListener
to add a conversation listener, it will receive the onConversationChanged
callback and update the UI.onRecvC2CReadReceipt
callback that contains the timestamp when the conversation unread count is cleared.public void onConversationChanged(List<V2TIMConversation> conversationList) {// The caller received the notification of a change in the conversation information.Log.i("imsdk", "onConversationChanged");}public void onRecvC2CReadReceipt(List<V2TIMMessageReceipt> receiptList) {// Read receipt for the one-to-one messageLog.i("imsdk", "onRecvC2CReadReceipt");}
// Add a conversation listener[[V2TIMManager sharedInstance] addConversationListener:self];// The caller received the notification of a change in the conversation information.- (void)onConversationChanged:(NSArray<V2TIMConversation*> *)conversationList {// Update the UI based on the `V2TIMConversation` in `conversationList`, for example, clear the badge in the cell of the one-to-one conversation}// Read receipt for the one-to-one message- (void)onRecvC2CReadReceipt:(NSArray<V2TIMMessageReceipt *> *)receiptList {}
class ConversationListener final : public V2TIMConversationListener {public:/*** If the key information of some conversations changes (for example, the unread count changes, or the last message is updated),* use "lastMessage -> timestamp" to sort the conversation list again.** @param conversationList Conversation list*/void OnConversationChanged(const V2TIMConversationVector& conversationList) override {}// Other members …};// Add a conversation event listener. Keep `conversationListener` valid before the listener is removed to ensure event callbacks are received.ConversationListener conversationListener;V2TIMManager::GetInstance()->GetConversationManager()->AddConversationListener(&conversationListener);class AdvancedMsgListener final : public V2TIMAdvancedMsgListener {public:/*** One-to-one message read notification (This callback is received when the recipient calls the `MarkC2CMessageAsRead` API.* The callback contains only the UserID and read timestamp information of the recipient.)** @param receiptList Read receipt list*/void OnRecvC2CReadReceipt(const V2TIMMessageReceiptVector& receiptList) override {}// Other members …};// Add an event listener for advanced messages. Keep `advancedMsgListener` valid before it is removed to ensure event callbacks are received.AdvancedMsgListener advancedMsgListener;V2TIMManager::GetInstance()->GetMessageManager()->AddAdvancedMsgListener(&advancedMsgListener);
markGroupMessageAsRead
(Android / iOS and macOS / Windows) to clear the unread message count of a specified group conversation.String groupID = "groupID";V2TIMManager.getMessageManager().markGroupMessageAsRead(groupID, new V2TIMCallback() {@Overridepublic void onSuccess() {Log.i("imsdk", "success");}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
[[V2TIMManager sharedInstance] markGroupMessageAsRead:@"conversationID" // ID of the target group conversationsucc:^{// Cleared the unread count successfully} fail:^(int code, NSString *msg) {// Failed to clear the unread count}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString groupID = u8"groupID";auto callback = new Callback;callback->SetCallback([=]() {// Cleared the unread count successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Cleared the unread count successfullydelete callback;});V2TIMManager::GetInstance()->GetMessageManager()->MarkGroupMessageAsRead(groupID, callback);
markGroupMessageAsRead
is called successfully, if the caller has called addConversationListener
to add a conversation listener, it will receive the onConversationChanged
callback and update the UI.public void onConversationChanged(List<V2TIMConversation> conversationList) {// The caller received the notification of a change in the conversation information.Log.i("imsdk", "onConversationChanged");}
// Add a conversation listener[[V2TIMManager sharedInstance] addConversationListener:self];// The caller received the notification of a change in the conversation information.- (void)onConversationChanged:(NSArray<V2TIMConversation*> *)conversationList {// Update the UI based on the `V2TIMConversation` in `conversationList`, for example, clear the badge in the cell of the group conversation}
class ConversationListener final : public V2TIMConversationListener {public:void OnConversationChanged(const V2TIMConversationVector& conversationList) override {}// The caller received the notification of a change in the conversation information.};// Add a conversation event listener. Keep `conversationListener` valid before the listener is removed to ensure event callbacks are received.ConversationListener conversationListener;V2TIMManager::GetInstance()->GetConversationManager()->AddConversationListener(&conversationListener);
markAllMessageAsRead
(Android / iOS and macOS / Windows) to clear the unread message count of all conversations.V2TIMManager.getMessageManager().markAllMessageAsRead(new V2TIMCallback() {@Overridepublic void onSuccess() {Log.i("imsdk", "success");}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
[[V2TIMManager sharedInstance] markAllMessageAsRead:^{// Cleared the unread count successfully} fail:^(int code, NSString *desc) {// Failed to clear the unread count}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};auto callback = new Callback;callback->SetCallback([=]() {// Cleared the unread count successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to clear the unread countdelete callback;});V2TIMManager::GetInstance()->GetMessageManager()->MarkAllMessageAsRead(callback);
markAllMessageAsRead
is called successfully, if the caller has called addConversationListener
in advance to add a conversation listener, it will receive the onConversationChanged
callback and update the UI.public void onConversationChanged(List<V2TIMConversation> conversationList) {// Received the notification of a change in the conversation informationLog.i("imsdk", "onConversationChanged");}
// Add a conversation listener[[V2TIMManager sharedInstance] addConversationListener:self];// The caller received the notification of a change in the conversation information.- (void)onConversationChanged:(NSArray<V2TIMConversation*> *)conversationList {// Update the UI, for example, clear the badge on the bottom tab of the conversation list}
class ConversationListener final : public V2TIMConversationListener {public:void OnConversationChanged(const V2TIMConversationVector& conversationList) override {}// The caller received the notification of a change in the conversation information.};// Add a conversation event listener. Keep `conversationListener` valid before the listener is removed to ensure event callbacks are received.ConversationListener conversationListener;V2TIMManager::GetInstance()->GetConversationManager()->AddConversationListener(&conversationListener);
unreadCount
of the V2TIMConversation
conversation object.
If you want to send messages that will not be included in the unread count, such as tips or control messages, when calling sendMessage
, you can:setExcludedFromUnreadCount
and set it to true
.isExcludedFromUnreadCount
to YES
/true
.setExcludedFromUnreadCount
or isExcludedFromUnreadCount
parameter is supported only by the SDK of the Enhanced edition on v5.3.425 or later.// Create the message objectV2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createTextMessage(content);// Set not to update the `lastMessage` of the conversationv2TIMMessage.setExcludedFromUnreadCount(true);// Send the messageV2TIMManager.getMessageManager().sendMessage(v2TIMMessage, "userID", null, V2TIMMessage.V2TIM_PRIORITY_DEFAULT, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onSuccess(V2TIMMessage v2TIMMessage) {Log.i("imsdk", "success");}@Overridepublic void onProgress(int progress) {Log.i("imsdk", "progress:" + progress);}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
// Create the message objectV2TIMMessage *message = [[V2TIMManager sharedInstance] createTextMessage:@"This is a signaling message"];// Set the identifier for excluding from the unread message countmessage.isExcludedFromUnreadCount = YES;// Send the message[[V2TIMManager sharedInstance] sendMessage:msg receiver:@"userA" groupID:nilpriority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:YES offlinePushInfo:nil progress:^(uint32_t progress) {} succ:^{// Message sent successfully} fail:^(int code, NSString *msg) {// Failed to send the message}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// Create the message objectV2TIMMessage message = V2TIMManager::GetInstance()->GetMessageManager()->CreateTextMessage(u8"content");// Set not to update the `lastMessage` of the conversationmessage.isExcludedFromUnreadCount = true;auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) { delete callback; },[=](int error_code, const V2TIMString& error_message) { delete callback; },[=](uint32_t progress) {});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(message, u8"userID", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
Was this page helpful?