Mohit Gandhi
Mohit Gandhi

Reputation: 19

How to get Unconsumed message count

Hello I am integrating Twilio on android and I need to display unconsumed message count with the channel name below is my code snippet but every time I'm getting count zero(0) please help me

channel.getUnconsumedMessagesCount(new CallbackListener<Long>() {
@Override
public void onSuccess(Long aLong) {
    unreadCountTextView.setText(String.valueOf(aLong));
}
    });

Upvotes: 0

Views: 1178

Answers (2)

Ali Azaz Alam
Ali Azaz Alam

Reputation: 1868

According to new Twilio Conversation API, progress listener is now gone so, you could calculate count in this way:

This code would work if you have only one channel or multiple channels so, it would return count of all conversation unconsummed messages.

Kotlin:

lifecycleScope.launch {
      val messageChannel = Channel<Int>()
      val allChannels = (get my conversation from conversationClient)
      allChannels?.let { item ->
              item.forEach { channel ->
                   // Get unconsummed message count
                   channel.getUnreadMessagesCount { count ->
                        count?.let { no ->
                              launch {messageChannel.send(no.toInt())}
                        }
                   }
               }
       
                //Observe channel to assure that above callbacks returned data
                var msgsCount = 0
                val messageReceive = launch {
                         repeat(item.size) {
                               msgsCount += messageChannel.receive()
                         }
                }
                messageReceive.join()
     
                log.i("Total message count", msgsCount) 
      }
 }

Upvotes: 0

lizziepika
lizziepika

Reputation: 3300

Twilio developer evangelist here.

Looks like you might have to set a Consumption Horizon on a channel--Chat does not automatically do that for you. From the docs on Consumption Horizon, "If a user does not have a Consumption Horizon set on a channel, getting unconsumed messages will always return 0."

Upvotes: 2

Related Questions