Skip to main content

Retrieving Chatroom History Messages

The client SDK supports retrieving chatroom history messages with the following capabilities:

  • Use getHistoryMessages to fetch chatroom history messages stored locally. Note that by default, the chatroom service clears locally stored history messages when a user exits the chatroom.
  • Use getChatroomHistoryMessages to retrieve chatroom history messages stored on the server.

Retrieving Remote Chatroom History Messages

Call the getChatroomHistoryMessages method. If the local database contains history messages from the same time period, this interface will return an array with a size of 0. Therefore, the recommended calling sequence is as follows:

  1. First, call RongCoreClient's getHistoryMessages, passing in the conversation type, chatroom ID, the ID of the last message, and specifying the number of messages to retrieve, among other parameters, to fetch history messages from the local message database.
  2. If the result is empty, then call RongChatRoomClient's getChatroomHistoryMessages method, passing in the chatroom ID, the message's timestamp, the query direction, and specifying the number of messages to retrieve, among other parameters, to fetch history messages from the chatroom message cloud storage. When the query direction remains unchanged, the syncTime returned in the current call can be used as the recordTime for the next pull, facilitating continuous retrieval.

Interface

RongCoreClient.getInstance().getHistoryMessages(conversationType, targetId, oldestMessageId, count,callback);

Parameter Description

ParameterTypeDescription
conversationType[ConversationType]Conversation type
targetIdStringConversation ID
oldestMessageIdLongThe ID of the last message. Set to -1 to query the latest messages in the local database.
countIntNumber of messages per page.
callbackResultCallback<List<Message>>Callback for retrieving history messages, ordered chronologically from newest to oldest.

Example Code

Conversation.ConversationType conversationType = Conversation.ConversationType.CHATROOM;
int oldestMessageId = -1;
final int count = 10;

final String targetId = "Chatroom ID";
final long recordTime = 0;

RongCoreClient.getInstance().getHistoryMessages(conversationType, targetId, oldestMessageId, count,
new IRongCoreCallback.ResultCallback<List<Message>>() {

@Override
public void onSuccess(List<Message> messages) {
if (messages == null || messages.isEmpty()) {
RongChatRoomClient.getInstance().getChatroomHistoryMessages(targetId, recordTime, count, IRongCoreEnum.TimestampOrder.RC_TIMESTAMP_ASC,
new IRongCoreCallback.IChatRoomHistoryMessageCallback() {

@Override
public void onSuccess(List<Message> messages, long syncTime) {

}


@Override
public void onError(IRongCoreEnum.CoreErrorCode code) {

}
});

}
}

@Override
public void onError(IRongCoreEnum.CoreErrorCode e) {

}
});

The success callback of getChatroomHistoryMessages returns a syncTime. The specific meaning of syncTime depends on the value of the order parameter in getChatroomHistoryMessages.

  • If the retrieval order is RC_Timestamp_Desc, it indicates a descending query direction, meaning messages with timestamps earlier than recordTime are fetched in descending order of their send times. syncTime is the timestamp of the earliest message in the result (i.e., the smallest timestamp).
  • If the retrieval order is RC_Timestamp_Asc, it indicates an ascending query direction, meaning messages with timestamps later than recordTime are fetched in ascending order of their send times. syncTime is the timestamp of the latest message in the result (i.e., the largest timestamp).