skip to Main Content

Currently unread messages count is 0 and want to get unread messages count for each conversation.

I did used conversation.lastMessageReadIndex but it ask for return results to listener. Don’t know which listener we can use for that?

2

Answers


  1. Try this code snippet

    // Create a ConversationListener object
    ConversationListener conversationListener = new ConversationListener() {
        @Override
        public void onConversationUpdated(Conversation conversation, Conversation.UpdateReason updateReason) {
            // Get the unread message count for the conversation
            int unreadMessageCount = conversation.getLastMessageIndex() - conversation.getLastReadMessageIndex();
            Log.d("TAG", "Unread message count for conversation " + conversation.getSid() + ": " + unreadMessageCount);
        }
    };
    
    // Add the conversationListener to the ConversationsClient
    conversationsClient.addConversationListener(conversationListener);
    
    Login or Signup to reply.
  2. I just did this…I don’t know if twilio SDK provides a better way to do this…At least I didn’t find it. I assume this is what you meant by "each conversation", my understanding it’s that you want to find the total unread count of all of your conversations.

    suspend fun getTotalUnreadMessageCount(list: List<Conversation>): Long {
        var totalCount = 0L
        for (conversation in list) {
            val count = conversation.getUnreadMessagesCount() //THIS MIGHT BE NULL
            if (count != null) {
                totalCount += count
            }
        }
        return totalCount
    }
    

    Suspend function because getUnreadMessageCount is also suspend, so you have to use this function in a coroutine.

    It’s just fetching unread count for each conversation and adding it to the total unread amount if it’s not null. You can also do a ?: 0 to dismiss the if null check

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search