检索对话列表

您可以检索用户的对话列表。对话是指用户发送了消息或直接消息 (DM) 的频道。您可以使用此 API 为用户填充频道或 DM 列表,以便他们找到自己的活动聊天。

Note: The returned Conversation list is ranked by most recent activity. Conversations with newer messages show up first in the results.

要检索对话列表,请登录应用程序,然后使用 [await VivoxService.Instance.GetConversationsAsync();](https://docs.unity3d.org.cn/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_GetConversationsAsync_Unity_Services_Vivox_ConversationQueryOptions_) 发出请求。此方法返回VivoxConversation` 对象的集合,每个对象代表一个 DM 或一个频道对话。

以下代码示例演示如何检索用户的对话列表。

// The use of `ConversationQueryOptions` is optional and the method can be called without providing it.
var options = new ConversationQueryOptions()
{
    CutoffTime = DateTime.Now, // Point in time to fetch conversations from. Conversations joined after this timestamp will not be present in the query results. This timestamp gets converted to UTC internally.
    PageCursor = 1, // Parameter if you’re fetching anything but the first page.
    PageSize = 10 // The number of results returned per page. The default is 10.
};
var conversations = await VivoxService.Instance.GetConversationsAsync(options);

检索用户的对话列表后,您可以解析 VivoxConversation 的结果集合,以确定哪些是 DM,哪些是频道对话。

foreach (VivoxConversation conversation in conversations)
{
    switch (conversation.ConversationType)
    {
        case ConversationType.ChannelConversation:
            // Code for handling channel conversations.
            // You must be in a channel matching conversation.Name in order to fetch the message history of type ConversationType.ChannelConversation.
            break;
        case ConversationType.DirectedMessageConversation:
            // Code for handling DM conversations, for instance...
            var directMessages = await VivoxService.Instance.GetDirectTextMessageHistoryAsync(conversation.Name);
            // Populate UI with directMessages with another user.
            break;
        default:
            break;
    }
}

Note: Depending on the VivoxConversation.ConversationType of a returned VivoxConversation, the VivoxConversation.Name will either be the PlayerId of another user, or the unique channel name of a channel. In both cases, the VivoxConversation.Name is a unique identifier for one of the two types of conversations.