# 频道消息

> Send, receive and manage text messages in Vivox channels.

VivoxMessages 可以发送到使用 TextOnly 或 TextAndAudio 的 ChatCapability 启用了文本的特定 Vivox 频道。

## 发送频道消息##send-channel-messages

您可以使用 [`VivoxService.Instance.SendChannelTextMessageAsync(string channelName, string message)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_SendChannelTextMessageAsync_System_String_System_String_Unity_Services_Vivox_MessageOptions_) 向频道发送消息，其中 `channelName` 是消息的目标频道，`message` 是要发送的消息。

以下代码片段的示例实现了将消息发送到设置的 Lobby 频道并从 MessageInputField InputField 对象提取文本。

```cs
public async void SendMessageAsync()
{
    if (string.IsNullOrEmpty(MessageInputField.text))
    {
        return;
    }

    VivoxService.Instance.SendChannelTextMessageAsync(LobbyChannelName, MessageInputField.text);
    MessageInputField.text = string.Empty;
}
```

## 接收频道消息##receive-channel-messages

为了从启用了文本的频道接收消息，必须订阅 [`VivoxService.Instance.ChannelMessageReceived`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_ChannelMessageReceived) 操作。

发送到频道的 VivoxMessages 与[定向消息](./directed-messages.md)几乎相同，只是在 `VivoxMessage.ChannelName` 中添加了 ChannelName，并根据消息是否来自当前登录用户设置了 `VivoxMessage.FromSelf` 字段。

以下代码片段是订阅以上操作的代码行示例，以及在收到 VivoxMessage 时从中提取所有可用信息的示例。

```cs

//Subscribe to this event at some point in the Vivox initialization process,
//preferably before joining any channels.
. . .
VivoxService.Instance.ChannelMessageReceived += OnChannelMessageReceived;
. . .

void OnChannelMessageReceived(VivoxMessage message)
{
    var channelName = message.ChannelName;
    var senderName = message.SenderDisplayName;
    var senderId = message.SenderPlayerId;
    var messageText = message.MessageText;
    var timeReceived = message.ReceivedTime;
    var language = message.Language;
    var fromSelf = message.FromSelf;
    var messageId = message.MessageId;
}
```

## 频道文本消息历史记录##channel-text-message-history

Vivox 允许用户使用 [`VivoxService.Instance.GetChannelTextMessageHistoryAsync(string channelName, int requestSize = 10, ChatHistoryQueryOptions chatHistoryQueryOptions = null)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_GetChannelTextMessageHistoryAsync_System_String_System_Int32_Unity_Services_Vivox_ChatHistoryQueryOptions_) 访问频道文本活动的历史记录，其中 `channelName` 是要请求相应历史记录的频道的名称，`requestSize` 是最多要返回的消息数（默认值为 10），`chatHistoryQueryOptions` 是一个可选参数，可以执行多种操作，例如指定消息中包含的单词或短语、消息的特定 PlayerId 发送方，或前后查询时间。如需了解有关 `ChatHistoryQueryOption`s 的更多具体信息，请参阅[聊天历史记录查询选项](../../text-chat-guide/chat-history-query-options)。

`IReadOnlyCollection` 中返回的消息按从最新消息到最旧消息的顺序列出。

> **Note:**
>
> 聊天历史记录保存 7 天。如果正在使用[文本证据管理](../../text-chat-guide/text-evidence/text-evidence-management)，聊天历史记录的保存期限将延长至 30 天。

以下代码片段的示例说明如何从设置的 LobbyChannelName 中提取最近 25 条消息，然后按从旧到新的顺序记录发送方的显示名称和消息，而无需使用可选的 ChatHistoryQueryOptions。

```cs
public async void FetchHistoryAsync()
{
    var historyMessages = await VivoxService.Instance.GetChannelTextMessageHistoryAsync(LobbyChannelName, 10);
    //Reversing the messages so they display from oldest to newest
    var reversedMessages = historyMessages.Reverse();
    foreach(VivoxMessage message in reversedMessages)
    {
        Debug.Log($"{message.SenderDisplayName}: {message.MessageText}");
    }
}
```

## 编辑频道消息##edit-channel-messages

Vivox 允许用户使用 [`VivoxService.Instance.EditChannelTextMessageAsync(string channelName, string messageId, string newMessage)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_EditChannelTextMessageAsync_System_String_System_String_System_String_) 编辑他们发送到频道的消息文本，其中 channelName 是消息发送到的频道的名称，messageId 是要更改的消息的 ID，newMessage 是更新的消息文本。

当频道中的任何人成功编辑消息后，频道中的所有人都将收到一个 [`VivoxService.Instance.ChannelMessageEdited`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_ChannelMessageEdited) 操作，其中包含已编辑的 VivoxMessage 以及更新的 MessageText。

```cs
public async void UpdateChannelMessageAsync(VivoxMessage messageToUpdate, string updatedMessageText)
{
    await VivoxService.Instance.EditChannelTextMessageAsync(messageToUpdate.ChannelName, messageToUpdate.MessageId, updatedMessageText);
}
```

## 删除频道消息##delete-channel-messages

Vivox 允许用户使用 [`VivoxService.Instance.DeleteChannelTextMessageAsync(string channelName, string messageId)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DeleteChannelTextMessageAsync_System_String_System_String_) 删除他们发送到频道的消息，其中 channelName 是消息发送到的频道的名称，messageId 是要删除的消息的 ID。

当频道中的任何人成功删除消息后，频道中的所有人都将收到一个 [`VivoxService.Instance.ChannelMessageDeleted`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_ChannelMessageDeleted) 操作，其中包含已删除的 VivoxMessage。

```cs
public async void DeleteChannelMessageAsync(VivoxMessage messageToDelete)
{
    await VivoxService.Instance.DeleteChannelTextMessageAsync(messageToDelete.ChannelName, messageToDelete.MessageId)
}
```
