0

I want to display next to a chat channel the number of messages a channel has that have been unconsumed or unread (I assume this is what unconsumed means?)

Currently I send messages to a channel that two users are subscribed to , a private chat. Then before opening up the chat window I check the channel for unconsumed messages, but it always say 0 messages even if I call setNoMessagesConsumedWithCompletion.

I am using the Swift API...What do I need to do to find out how many messages in my channel have not been read yet? At what point do they become read? (when the user opens up a chat channel and requests to getLastWithCount?)

I read in the docs you have to set something called the consumption horizon to get unconsumed message, but I don't know how you do that in SWIFT API https://www.twilio.com/docs/chat/consumption-horizon also this was for Javascript API so perhaps it is easier with Swift Api?

Ben Smith
  • 521
  • 3
  • 15

1 Answers1

0

I figured out the solution. As per the documentation you need to update the last consumed message index. So for example if the user has a chat window open you need to record for that user (or instance of the Chat Client) what was the last message they saw before they close their chat. I am storing all the messages in a message array and update the last consumed message index with the length of the array of messages:

generalChannel?.messages?.setLastConsumedMessageIndex(NSNumber.init(value: self.messages.count), completion: { (result, count) in
        if !result.isSuccessful() {
            print(result.error.debugDescription)
        }
    })

Then if you send messages to that channel when the user is not in the channel these will be recorded as unconsumed, you can get the number by calling:

channel.getUnconsumedMessagesCount(completion: { (results, numberUnconsumed) in
                        print(numberUnconsumed)
                    })
Ben Smith
  • 521
  • 3
  • 15
  • 2
    I think message index can't be same as total messages count all the time. The best solution is to get the index of the last message loaded and pass it through this function – Yasir Tahir Apr 03 '19 at 13:21
  • Can anyone help me? I am facing similar problem when I create a group and send message, then on other member's device, the unread message count is not updating to number of messages I am sending until user reads messages. – mohinimehetre Oct 27 '20 at 11:31