Mark all unread messages a read

Is there a possibility to mark ALL unread Messages as read in conversation in Android v2?

1 Like

@floriansenf We have the following method to mark messages as read:

CometChat.markAsRead(message.getId(),message.getSender().getUid(),CometChatConstants.RECEIVER_TYPE_USER);

This method will mark all the messages before the messageId specified, for the conversation with receiverId and receiverType (user/group) as read.

Okay so not only one message get marked but all Messages before the message id in the function get marked as read?

1 Like

also I have one more Question: I want to get unreadmessagecountforusers and unreadmessagescountforgroups in 2 different functions at the same time. Now if I send a message in a private Chat the hashmap in the groupcount hashmap returns also one?

1 Like

Yes @floriansenf
That is correct

@floriansenf
Can you please elaborate on the query regarding the unread messages so that we can help you better and provide you with a solution at the earliest.

Here is my Code. Now when I send a private message both Methods are called and return the same numer ,but it should be only called in ForAllUsers or am I wrong?
CometChat.getUnreadMessageCountForAllUsers(true, new CometChat.CallbackListener<HashMap<String, Integer>>() {
@Override
public void onSuccess(HashMap<String, Integer> stringIntegerHashMap) {
Log.e(“response1”,stringIntegerHashMap.toString());
for (String key : stringIntegerHashMap.keySet()) {
chatall = chatall + stringIntegerHashMap.get(key);

            }

            if (chatall != 0) {
                if (chatall < 100) {
                    if (chatmessagecount.getVisibility() == View.INVISIBLE) {
                        chatmessagecount.setText(String.valueOf(chatall));
                        chatmessagecount.setVisibility(View.VISIBLE);
                    }

                } else {
                    if (chatmessagecount.getVisibility() == View.INVISIBLE) {
                        chatmessagecount.setText("99+");
                        chatmessagecount.setVisibility(View.VISIBLE);
                    }

                    chatall = 100;
                }

            } else {
                chatall = 0;
            }
        }

        @Override
        public void onError(CometChatException e) {

        }
    });
    CometChat.getUnreadMessageCountForAllGroups(true, new CometChat.CallbackListener<HashMap<String, Integer>>() {
        @Override
        public void onSuccess(HashMap<String, Integer> stringIntegerHashMap) {

            for (String key : stringIntegerHashMap.keySet()) {
                groupall = groupall + stringIntegerHashMap.get(key);

            }
            if (groupall != 0) {
                if (groupall < 100) {
                    if(groupmessagecount.getVisibility()==View.INVISIBLE){
                        groupmessagecount.setText(String.valueOf(groupall));
                        groupmessagecount.setVisibility(View.VISIBLE);
                    }

                } else {
                    if(groupmessagecount.getVisibility()==View.INVISIBLE){
                        groupmessagecount.setText("99+");
                        groupmessagecount.setVisibility(View.VISIBLE);
                    }

                    groupall = 100;
                }

            } else {
                groupall = 0;
            }
        }

        @Override
        public void onError(CometChatException e) {

        }
    });
}
1 Like

Hello @floriansenf,
Just to confirm, you are calling the getUnreadMessagesForAllUsers and getUnreadMessagesForAllGroups after sending a message…??
Ideally, when a user receives messages, the unread message count is increased by the number of messages received and based on if the messages are received for user conversations or group, the count of unread messages is returned in the respective method getUnreadMessagesForAllUsers or getUnreadMessagesForAllGroups.
Also, please make sure you are marking the messages as read correctly so that the correct value of count is maintained. Please refer to our sample app below to check how this is implemented:

Also, just a suggestion, if you want the unread count for both users and groups, you can obtain them in a single method getUnreadMessageCount() instead of two separate calls.
Please refer to the below link for more information:

Hope this helps. If you still have any doubts, please let us know and we will be happy to help you.

Iam not calling them directly after sending a message. After sending a group message and I open a new Activity and call both Methods at the same time, both methods return the same hashmap even if I send only 1 message into a group. In countforalluser the hasmap contains a guid and the message Count for the group.
So i don’t understand how to fetch unreadmessagecount for only one to one conversations

1 Like

Hi @floriansenf,

The issue has been fixed at our API Server.

Please use CometChat.getUnreadMessageCountForAllUsers(Callbacks); method to get unread message count for all one-to-one conversation and CometChat.getUnreadMessageCountForUser(String UID, Callbacks); to get unread message count of specific conversation.

For more information on UnreadMessageCount, you can visit our Documentation https://prodocs.cometchat.com/docs/android-messaging-receive-messages#section-unread-messages-count

1 Like

Okay nice! I am glad that I helped you improving the api performance. It works now

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.