问题描述
我正在尝试获取特定频道中特定成员的未读消息数。为此,我希望使用。
I'm trying to get the number of messages unread for a specific member in a specific channel. To do this I was hoping to use channel.getUnconsumedMessagesCount() as defined in the documentation.
myChannel.join()
.then(function(c) {
console.log('Joined channel ' + c.sid);
return myChannel.getUnconsumedMessagesCount();
})
.then(m => {
console.log('current count unread: ' + m);
});
未读计数总是返回0.要测试,我会执行以下操作:
The unread count always return 0. To test, I do the following:
- 用户2在另一个Chrome选项卡中向myChannel提交消息
- 用户2 myChannel获取更新来自(1)的消息通过
.on('messageAdded',[...]) - 刷新用户1 chrome标签,获取
值为0的getUnconsumedMessagesCount - 如果我为user1调用myChannel.getMessages(),我会看到来自user2的消息
- user 2 submitting a message to myChannel in another chrome tab
- user 2 myChannel get's updated with the message from (1) via.on('messageAdded', [...])
- refresh user 1 chrome tab, and get getUnconsumedMessagesCount withvalue of 0
- If I call myChannel.getMessages() for user1, I see the message from user2
最初我调用了.getUnconsumedMessagesCount()没有首先做join(),我认为这可能是问题,但即使加入仍然没有。
Initially I called .getUnconsumedMessagesCount() without doing join() first, I thought this could be the issue, but even with join still nothing.
推荐答案
我对javascript SDK遇到同样的问题,它总是返回0。
I am having the same issue with the javascript SDK where it always returns 0.
我最终做了以下工作
const count = channel.getMessagesCount();
const unreadCount = channel.lastConsumedMessageIndex === null ? count : count - channel.lastConsumedMessageIndex - 1
我还注意到使用任何函数来设置消耗的消息可能需要一整分钟才能在channel.lastConsumedMessageIndex
I also noticed that using any of the functions to set the messages consumed can take up to a full minute before actually returning that information in the channel.lastConsumedMessageIndex
这篇关于Twilio Chat API,getUnconsumedMessagesCount始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!