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 ChatManager
to manage local messages. Followings are the core methods:
loadAllConversations
: Loads the conversation list on the local device.deleteConversation
: Deletes the specified conversation.Conversation.getUnreadMsgCount
: Retrieves the count of unread messages in the specified conversation.getUnreadMessageCount
: Retrieves the count of all unread messages.deleteConversation
: Deletes the conversation from the server.searchMsgFromDB
: Searches for messages from the local database.Conversation.insertMessages
: 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:
Map<String, Conversation> conversations = ChatClient.getInstance().chatManager().getAllConversations();
Refer to the following code sample to retrieve the messages in the specified conversation:
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(username);
// Gets all the messages in the current conversation.
List<ChatMessage> messages = conversation.getAllMessages();
// Only one message is loaded during SDK initilization. Call loadMoreMsgFromDB to retrieve more messages.
List<ChatMessage> messages = conversation.loadMoreMsgFromDB(startMsgId, pagesize);
Refer to the following code example to retrieve the count of unread messages:
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(username);
conversation.getUnreadMsgCount();
Refer to the following code example to retrieve the count of all unread messages:
ChatClient.getInstance().chatManager().getUnreadMessageCount();
Refer to the following code example to mark the specified messages as read:
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(username);
// Mark all the messages in the current conversation as read.
conversation.markAllMessagesAsRead();
// Mark the specified message as read.
conversation.markMessageAsRead(messageId);
// Mark all unread messages as read.
ChatClient.getInstance().chatManager().markAllConversationsAsRead();
You can delete conversations on both the local device and the server.
To delete them on the local device, call deleteConversation
and removeMessage
:
// true indicates to keep the historical messages while deleting the conversation. To remove the historical messages as well, set it as false.
ChatClient.getInstance().chatManager().deleteConversation(username, true);
// Delete the specified message in the current conversaiton.
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(username);
conversation.removeMessage(deleteMsg.msgId);
Call searchMsgFromDB
to search for messages by keywords, timestamp, and message sender:
List<ChatMessage> messages = conversation.searchMsgFromDB(keywords, timeStamp, maxCount, from, Conversation.SearchDirection.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.
ChatClient.getInstance().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 insertMessages
. This can be used to send notification messages such as "XXX recalls a message", "XXX joins the chat group", and "Typing ...".
// Insert a message to the specified conversation.
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(username);
conversation.insertMessage(message);
// Insert a message.
ChatClient.getInstance().chatManager().saveMessage(message);
After implementing managing messages, you can refer to the following documents to add more messaging functionalities to your app: