Skip to main content
Android
iOS
Web
Windows
Unity
Flutter
React Native

Manage local messages

In chat apps, a conversation is composed of all the messages in a peer-to-peer chat, chat group, or chatroom. The 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 Chat SDK to implement these functionalities.

Understand the tech

SQLCipher is used to encrypt the database that stores local messages. The Chat SDK uses ChatManager to manage local messages. Followings are the core methods:

  • getAllConversationsBySort: Loads the conversation list on the local device.
  • deleteConversation: Deletes the specified conversation locally.
  • deleteConversationFromServer: Delete a conversation from the server.
  • removeMessage: Deletes a message sent or received in a local conversation.
  • clearAllMessages:Deletes all messages sent and received in a local conversation
  • removeMessages: Deletes messages sent and received in a certain period in a local conversation.
  • Conversation.getUnreadMsgCount: Retrieves the count of unread messages in the specified conversation.
  • getUnreadMessageCount: Retrieves the count of all unread messages.
  • asyncPinConversation: Pins a conversation.
  • asyncFetchPinnedConversationsFromServer: Retrieves the pinned conversations from the server with pagination.
  • searchMsgFromDB: Searches for messages from the local database.
  • Conversation.insertMessage: Inserts messages in the specified conversation.

Prerequisites

Before proceeding, ensure that you meet the following requirements:

  • You have integrated the Chat SDK, initialized the SDK and implemented the functionality of registering accounts and login. For details, see Chat SDK quickstart.
  • You understand the API call frequency limits as described in Limitations.

Implementation

This section shows how to implement managing messages.

Retrieve conversations

You can call the getAllConversationsBySort method to get all local conversations. The SDK first retrieves the conversations from the memory. If no conversation is loaded from the local database, the SDK will load the conversations to the memory. The SDK returns the conversation list in the reverse chronological order of when conversations are active (the timestamp of the last message in the conversation), with the pinned conversations coming before the unpinned ones. The conversation list is of the List<EMConversation> structure:


_1
List<Conversation> conversations = ChatClient.getInstance().chatManager().getAllConversationsBySort();

Retrieve messages in the specified conversation

Call getAllMessages to retrieve all the messages of this conversation in the memory. Alternatively, you can call loadMoreMsgFromDB to load messages from the local database. The loaded message will be placed in the memory based on the timestamp of the messages.


_6
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(conversationId);
_6
_6
List<ChatMessage> messages = conversation.getAllMessages();
_6
// startMsgId: Starting message ID for query. The SDK loads messages, starting from the specified one, in the descending order of the timestamp included in the messages.
_6
// pageSize: Number of message to load on each page. The value range is [1,400].
_6
List<ChatMessage> messages = conversation.loadMoreMsgFromDB(startMsgId, pagesize);

Retrieve the count of unread messages in the specified conversation

Refer to the following code example to retrieve the count of unread messages:


_2
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(conversationId);
_2
conversation.getUnreadMsgCount();

Retrieve the count of unread messages in all conversations

Refer to the following code example to retrieve the count of all unread messages:


_1
ChatClient.getInstance().chatManager().getUnreadMessageCount();

Mark unread messages as read

Refer to the following code example to mark the specified messages as read:


_7
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(conversationId);
_7
// Mark all the messages in the current conversation as read.
_7
conversation.markAllMessagesAsRead();
_7
// Mark the specified message as read.
_7
conversation.markMessageAsRead(messageId);
_7
// Mark all unread messages as read.
_7
ChatClient.getInstance().chatManager().markAllConversationsAsRead();

Delete conversations and historical messages

You can delete conversations on both the local device and the server.

To delete them on the local device, call deleteConversation and removeMessage:


_5
// true indicates to keep the historical messages while deleting the conversation. To remove the historical messages as well, set it as true.
_5
ChatClient.getInstance().chatManager().deleteConversation(conversationId, deleteMessages);
_5
// Delete the specified message in the current conversation.
_5
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(conversationId);
_5
conversation.removeMessage(deleteMsg.msgId);

To delete a conversation on the server, call DeleteConversationFromServer:


_9
ChatClient.getInstance().chatManager().deleteConversationFromServer(conversationId, conversationType, isDeleteServerMessages, new CallBack() {
_9
@Override
_9
public void onSuccess() {
_9
_9
}
_9
@Override
_9
public void onError(int code, String error) {
_9
}
_9
});

Delete all messages in a local conversation

You can call clearAllMessages to delete all messages sent and received in a local conversation:


_5
// conversationId: The conversation ID, which is the user ID of the peer user in one-to-one chat, group ID in group chat, and chat room ID in room chat.
_5
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(conversationId);
_5
if(conversation != null) {
_5
conversation.clearAllMessages();
_5
}

Delete messages in a local conversation by time period

You can call removeMessages to delete messages sent and received in a certain period in a local conversation.


_7
// conversationId: The conversation ID, which is the user ID of the peer user in one-to-one chat, group ID in group chat, and chat room ID in room chat.
_7
// startTime: The starting UNIX timestamp for message deletion.
_7
// endTime: The end UNIX timestamp for message deletion.
_7
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(conversationId);
_7
if(conversation != null) {
_7
conversation.removeMessages(startTime, endTime);
_7
}

Search for messages using keywords

Call searchMsgFromDB to search for messages by keywords, timestamp, and message sender:


_1
List<ChatMessage> messages = conversation.searchMsgFromDB(keywords, timeStamp, maxCount, from, Conversation.SearchDirection.UP);

Import messages

Call importMessages to import multiple messages to the local database.


_1
ChatClient.getInstance().chatManager().importMessages(msgs);

Insert messages

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 ...".


_5
// Insert a message to the specified conversation.
_5
Conversation conversation = ChatClient.getInstance().chatManager().getConversation(conversationId);
_5
conversation.insertMessage(message);
_5
// Insert a message.
_5
// ChatClient.getInstance().chatManager().saveMessage(message);

Next steps

After implementing managing messages, you can refer to the following documents to add more messaging functionalities to your app:

vundefined