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
and ChatConversation
to manage local messages. Followings are the core methods:
ChatManager.getAllConversations
: Loads the conversation list on the local device.ChatManage.deleteConversation
: Deletes the specified conversation.ChatConversation.getConversationUnreadCount
: Retrieves the count of unread messages in the specified conversation.ChatManager.getUnreadCount
: Retrieves the count of all unread messages.ChatManager.removeConversationFromServer
: Deletes the conversation and historical messages from the server.ChatManager.searchMsgFromDB
: Searches for messages from the local database.
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
Call getAllConversations
to retrieve all the conversations on the local device:
Retrieve messages in the specified conversation
You can retrieve the messages in the specified conversation from the local database by specifying the conversation ID and chat type:
Retrieve the count of unread messages in the specified conversation
Refer to the following code example to retrieve the count of unread messages:
Retrieve the count of unread messages in all conversations
Refer to the following code example to retrieve the count of all unread messages:
Mark unread messages as read
Refer to the following code example to mark the specified messages as read:
You can also mark all unread messages of the specified conversation as read:
Delete conversations and historical messages
The SDK provides two methods, which enables you to delete the conversations and historical messages on the local device and on the server respectively.
To delete them on the local device, call deleteConversation
:
To delete them on the server, call removeConversationFromServer
:
Search for messages using keywords
Call SearchMsgFromDB
to search for messages by keywords, timestamp, and message sender:
Import messages
Call importMessages
to import multiple messages to the specified conversation. This applies to scenarios where chat users want to formard messages from another conversation.
Next steps
After implementing managing messages, you can refer to the following documents to add more messaging functionalities to your app: