Retrieve Chatroom History Messages
The client SDK supports retrieving chatroom history messages. The specific capabilities are as follows:
- Use getHistoryMessages to retrieve chatroom history messages stored locally. Please note that the chatroom service clears locally stored history messages by default when a user exits the chatroom.
- Use getChatroomHistoryMessages to retrieve chatroom history messages stored on the server. Please note that this feature relies on the Cloud Storage for Chatroom Messages service.
Enable the Service
Using getChatroomHistoryMessages requires enabling the Cloud Storage for Chatroom Messages service. Please ensure the service is enabled before use. Once enabled, chatroom history messages are stored in the cloud, with a default retention period of 2 months.
Retrieve Remote Chatroom History Messages
You can use getChatroomHistoryMessages to retrieve remote chatroom history records. If local database contains history messages from the same time period, calling this interface will return an array with a size of 0. Therefore, the recommended calling sequence is as follows:
- 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, to get history messages from the local message database.
- 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, to get history messages from the chatroom history message cloud storage. With the query direction unchanged, the
syncTime
returned in the current call can be used as therecordTime
for the next pull, facilitating continuous retrieval.
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
is related to the order
parameter value of getChatroomHistoryMessages
.
- If the retrieval order is
RC_Timestamp_Desc
, it means the query direction is descending, i.e., retrieving messages sent beforerecordTime
in order of decreasing message send time.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 means the query direction is ascending, i.e., retrieving messages sent afterrecordTime
in order of increasing message send time.syncTime
is the timestamp of the latest message in the result (i.e., the largest timestamp).