Skip to main content

Retrieve messages

The Chat SDK stores historical messages on the chat server. When a chat user logs in from a different device, you can retrieve the historical messages from the server, so that the user can also browse these messages on the new device.

This page introduces how to use the Chat SDK to retrieve messages from the server.

Understand the tech

The Chat SDK provides following methods to obtain historical messages from the server.

  • asyncFetchConversationsFromServerGet: A list of sessions saved by the server.
  • fetchHistoryMessagesGets: The messages in the specified session saved by the server.

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 retrieving conversations and messages.

Retrieve a list of conversations from the server

Call asyncFetchConversationsFromServer to retrieve all the conversations from the server. We recommend calling this method when the app is first installed, or when there is no conversation on the local device. Otherwise, you can call loadAllConversations to retrieve conversations on the local device.


_10
ChatClient.getInstance().chatManager().asyncFetchConversationsFromServer(new ValueCallBack<Map<String, Conversation>>() {
_10
// Add subsequent logic if retrieving conversation succeeds.
_10
@Override
_10
public void onSuccess(Map<String, Conversation> value) {
_10
}
_10
// Add subsequent logic if retrieving conversation fails.
_10
@Override
_10
public void onError(int error, String errorMsg) {
_10
}
_10
});

By default, the SDK retrieves the last ten conversations in the past seven days, and each conversation contains one last historical message. To adjust the time limit or the number of conversations retrieved, contact support@agora.io.

Retrieve historical messages of the specified conversation

After retrieving conversations, you can retrieve historical messages by pagination from the server.

You can set the search direction to retrieve messages in the chronological or reverse chronological order of when the server receives them.

To ensure data reliability, we recommend retrieving less than 50 historical messages for each method call. To retrieve more than 50 historical messages, call this method multiple times. Once the messages are retrieved, the SDK automatically updates these messages in the local database.


_11
ChatClient.getInstance().chatManager().asyncFetchHistoryMessage(conversationId, conversationType, pageSize, startMsgId, searchDirection, new ValueCallBack<CursorResult>() {
_11
@OverRide
_11
public void onSuccess(CursorResult value) {
_11
_11
}
_11
_11
@Override
_11
public void onError(int error, String errorMsg) {
_11
_11
}
_11
});

Next steps

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