logo
In-app Chat
SDK Error Codes
Powered Byspreading
On this page

Get message history


Function overview

This document describes how to query the message history and specific messages of one-to-one, group, and in-room chats with the ZIM SDK.

Note

This feature allows you to get historical messages of types other than command messages and pop-up messages.

Get the full message history

After logging in to ZIM SDK, users can use the queryHistoryMessage method to retrieve the message history of one-to-one chats, group chats, and rooms chats by providing the parameters conversationID and config.

Taking the example of client A retrieving the conversation history with client B in a one-on-one chat:

  1. Client A and B log in to the ZIM SDK and send/receive one-on-one chat messages to each other.
  2. When client A needs to retrieve the conversation records with B:
    1. Client A first logs in to the ZIM SDK.
    2. Client A calls the queryHistoryMessage interface and passes the conversationID and config parameters to start retrieving.
    3. The retrieved results will be notified to client A through the ZIMMessageQueriedCallback callback interface.
Sample code
// 1. Create a ZIM object and pass in the appID, appSign, and the Application in Android
ZIMAppConfig appConfig = new ZIMAppConfig();
appConfig.appID = 12345;  // Replace with your AppID
appConfig.appSign = "appSign";   // Replace with your AppSign
zim = ZIM.create(appConfig, application);

// 2. Log in
ZIMUserInfo zimUserInfo = new ZIMUserInfo();
zimUserInfo.userID = "xxxx";
zimUserInfo.userName = "xxxx";
zim.login(zimUserInfo, new ZIMLoggedInCallback() {
    @Override
    public void onLoggedIn(ZIMError error) {
          // Developers can use the ZIMError to determine if the login was successful.
    }
 });

// 3. Retrieve historical messages in a one-on-one chat
ArrayList<ZIMMessage> curMessageList = new ArrayList();

String conversationID = "xxxx";

// Retrieve 30 messages at a time, starting from the end
ZIMMessageQueryConfig config = new ZIMMessageQueryConfig();
// For the first retrieval, nextMessage is null
config.nextMessage = null;
config.messageCount = 30;
config.reverse = true;

ZIMMessageQueriedCallback callback = new ZIMMessageQueriedCallback() {
    @Override
    public void onMessageQueried(ArrayList<ZIMMessage> messageList, ZIMError errorInfo) {
        // Developers can use this callback to listen for the retrieved message list.

        curMessageList.addAll(0, messageList);

        // When scrolling down to the topmost message on the screen, retrieve earlier messages
        if (fetchMore) {
            // For subsequent pagination, nextMessage is the last message in the currently retrieved message list
            config.nextMessage = messageList.get(messageList.size() - 1);
            zim.queryHistoryMessage(conversationID, ZIMConversationType.Peer, config, callback);
        }
    }
}
zim.queryHistoryMessage(conversationID, ZIMConversationType.Peer, config, callback);
1
Copied!

Get specific messages

ZIM supports querying specific messages in a one-to-one or group conversation based on messageSeq (the sequence number of the message in the conversation) list (up to a maximum of 10) by calling queryMessages.

This interface is used when you only know the messageSeq of a message and do not know the complete structure of the message. For example, if a message in a conversation replies to a historical message, members of the conversation can use the repliedInfo.messageSeq of the reply to obtain the messageSeq of the historical message. At this time, you can call this interface to obtain the complete structure of the historical message.

Untitled
ArrayList<Long> messageSeqs = new ArrayList<>(); // The maximum length is 10                                              

String conversationID = "YOUR_CONVERSATION_ID";
ZIMConversationType conversationType = ZIMConversationType.PEER; // Conversation type: one-to-one: ZIMConversationType.PEER, group: ZIMConversationType.GROUP

ZIM.getInstance().queryMessages(messageSeqs, conversationID, conversationType, new ZIMMessageQueriedCallback() {
    @Override
    public void onMessageQueried(String conversationID, ZIMConversationType conversationType, ArrayList<ZIMMessage> messageList, ZIMError errorInfo) {
        if(errorInfo.code == ZIMErrorCode.SUCCESS) {
            // Query succeeded
        } else {
            // Query failed
        }
    }
});
1
Copied!

Previous

Send and receive messages

Next

Delete messages