In chat apps, a conversation is composed of all the messages in a peer-to-peer chat, chat group, or chatroom. The Agora Chat SDK supports managing messages by conversations, including retrieving and managing unread messages, deleting the historical messages on the local device, and searching historical messages.
This page introduces how to use the Agora Chat SDK to implement these functionalities.
SQLCipher is used to encrypt the database that stores local messages. The Agora Chat SDK uses IChatManager
and IConversationManager
to manage local messages. Followings are the core methods:
IChatManager.LoadAllConversations
: Loads the conversation list on the local device.IChatManage.DeleteConversation
: Deletes the specified conversation.IConversationManager.UnReadCount
: Retrieves the count of unread messages in the specified conversation.IChatManager.GetUnreadMessageCount
: Retrieves the count of all unread messages.IChatManager.DeleteConversationFromServer
: Deletes the conversation from the server.IChatManager.searchMsgFromDB
: Searches for messages from the local database.Conversation.insertMessage
: Inserts messages in the specified conversation.Before proceeding, ensure that you meet the following requirements:
This section shows how to implement managing messages.
Call LoadAllConversations
to retrieve all the conversations on the local device:
List<Conversation>list = SDKClient.Instance.ChatManager.LoadAllConversations();
Refer to the following code sample to retrieve the messages in the specified conversation:
// Get the specified conversation on the local device.
Conversation conv = SDKClient.Instance.ChatManager.GetConversation(conversationId, convType);
// Call LoadMessages to retrieve messages by specifying the `startMsgId` and `pageSize`.
conv.LoadMessages(startMsgId, pagesize, handle:new ValueCallBack<List<Message>>(
onSuccess: (list) => {
Debug.Log($"{list.Count} Messages retrieved.");
},
onError:(code, desc) => {
Debug.Log($"Fails to retrieve the message, errCode={code}, errDesc={desc}");
}
));
Refer to the following code example to retrieve the count of unread messages:
Conversation conv = SDKClient.Instance.ChatManager.GetConversation(conversationId, convType);
int unread = conv.UnReadCount;
Refer to the following code example to retrieve the count of all unread messages:
SDKClient.Instance.ChatManager.GetUnreadMessageCount();
Refer to the following code example to mark the specified messages as read:
Conversation conv = SDKClient.Instance.ChatManager.GetConversation(conversationId, convType);
// Mark all the messages in the current conversation as read.
conv.MarkAllMessageAsRead();
// Mark the specified message as read.
conv.MarkMessageAsRead(msgId);
// Mark all unread messages as read.
SDKClient.Instance.ChatManager.MarkAllConversationsAsRead();
You can delete conversations on both the local device and the server.
To delete them on the local device, call DeleteConversation
and DeleteMessage
:
// Call DeleteConversation to delete the specified conversation.
// `true` indicates to keep the historical messages while deleting the conversation. To remove the historical messages as well, set it as `false`.
SDKClient.Instance.ChatManager.DeleteConversation(conversationId, true);
// Delete the specified message in the current conversaiton.
Conversation conv = SDKClient.Instance.ChatManager.GetConversation(conversationId, convType);
conv.DeleteMessage(msgId);
To delete conversations on the server, call DeleteConversationFromServer
:
//从服务器端删除和特定 ID 的会话,如果需要保留聊天记录,第三个参数传 `false`。
SDKClient.Instance.ChatManager.DeleteConversationFromServer(conversationId, type, true, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));
Call SearchMsgFromDB
to search for messages by keywords, timestamp, and message sender:
List<Message> list = SDKClient.Instance.ChatManager.SearchMsgFromDB(keywords, timeStamp, maxCount, from, MessageSearchDirection.UP);
Call ImportMessages
to import multiple messages to the specified conversation. This applies to scenarios where chat users want to formard messages from another conversation.
SDKClient.Instance.ChatManager.ImportMessages(msgs);
If you want to insert a message to the current conversation without actually sending the message, construct the message body and call InsertMessage
. This can be used to send notification messages such as "XXX recalls a message", "XXX joins the chat group", and "Typing ...".
// Insert the message to the current conversation.
Conversation conv = SDKClient.Instance.ChatManager.GetConversation(conversationId, convType);
conv.InsertMessage(message);
After implementing managing messages, you can refer to the following documents to add more messaging functionalities to your app: