Kishore B
Kishore B

Reputation: 1

Twilio chat- getUnconsumedMessagesCount is returning null

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;
    }
  });
});

Upvotes: 0

Views: 1048

Answers (2)

Gianni Moschini
Gianni Moschini

Reputation: 101

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.

Upvotes: 2

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

Upvotes: 0

Related Questions