0

Twilio chat is returning getUnconsumedMessagesCount as null for all channel even channel has unread messages.

Any twilio expert here

The consumption horizon is updated with "setAllMessagesConsumed" and "updateLastConsumedMessageIndex" method still no luck.

This is working earlier in my application and the functionality is broken suddenly.

Below is the code

const clientChannel = await this.chatService.getChannel( channel_sid );

console.log('clientchamnel: ', clientChannel.status);
if (clientChannel.status !== 'joined') {
  await clientChannel.join();
}

this.lastMessage = { body: '' };
// get channels last message
clientChannel.getMessages().then(messages => {
  if (messages && messages.items && messages.items.length > 0) {
    this.lastMessage = messages.items[messages.items.length - 1];
  }
});



// get last unconsumedMessage so we can color the item if its read or not
clientChannel.getMessagesCount().then(totalCount => {
  console.log('total count:=' + totalCount);

  clientChannel.getUnconsumedMessagesCount().then(unconsumedCount => {
    console.log('unread count:=' + unconsumedCount);

    if (unconsumedCount != null && unconsumedCount > 0) {
      this.channel.unread = true;
    } else if (unconsumedCount == null && totalCount) {
      this.channel.unread = true;
    }
  });
});
Kishore B
  • 1
  • 1

2 Answers2

1

getUnconsumedMessagesCount will return null if the user has never called updateLastConsumedMessageIndex. I tend to rely on the getMessagesCount in that case (as a workaround), to get the number of messages unread.

  • You can also make a call Like channel.setNoMessagesConsumed(); with the help of event channelAdded.So whenever a new channel is added count will be set once & you won't receive it as null. – Khushbu Raval Jan 22 '21 at 13:06
0

I made it work calling setAllMessagesConsumed after user joins channel on the backend side, using the webhook feature.