如何使用android twilio对话SDK获得每次对话的未读消息计数?



当前未读消息计数为0,希望获得每个会话的未读消息计数

我确实使用了对话。lastMessageReadIndex,但它要求返回结果到监听器。不知道我们可以用哪个听众?

我刚刚做了这个…我不知道twilio SDK是否提供了更好的方法……至少我没有找到它。我猜这就是你所说的"每次谈话"的意思,我的理解是你想要找到所有谈话的未读总数。

suspend fun getTotalUnreadMessageCount(list: List<Conversation>): Long {
var totalCount = 0L
for (conversation in list) {
val count = conversation.getUnreadMessagesCount() //THIS MIGHT BE NULL
if (count != null) {
totalCount += count
}
}
return totalCount
}

挂起函数,因为getUnreadMessageCount也是挂起的,所以你必须在协程中使用这个函数

它只是为每个会话获取未读计数,并将其添加到总未读量,如果它不是空的。您还可以使用?:0来取消if null检查

试试下面的代码片段

// Create a ConversationListener object
ConversationListener conversationListener = new ConversationListener() {
@Override
public void onConversationUpdated(Conversation conversation, Conversation.UpdateReason updateReason) {
// Get the unread message count for the conversation
int unreadMessageCount = conversation.getLastMessageIndex() - conversation.getLastReadMessageIndex();
Log.d("TAG", "Unread message count for conversation " + conversation.getSid() + ": " + unreadMessageCount);
}
};
// Add the conversationListener to the ConversationsClient
conversationsClient.addConversationListener(conversationListener);

最新更新