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

Search for local messages


Overview

With the ZIM SDK, you can search for local messages of individual or group conversations by keywords, user ID, and other conditions. You can also search for conversations based on local messages.

Search local messages of a specific conversation

After creating a ZIM object and logging in, call the searchLocalMessages interface, pass in the parameters conversationID, conversationType, and config, and set the search conditions (such as keywords) to get the list of local messages that meet the conditions in a specific conversation.

The list of messages that meet the conditions will be returned through the ZIMMessagesSearchedResult callback interface and classified by the corresponding conversation.

Sample code
// Search for local text messages containing "zego" in a single conversation within the last 7 days.

// Calculate the timestamp of 7 days ago
int startTimestamp = DateTime.now().millisecondsSinceEpoch - (7 * 24 *60 *60 *1000);
int endTimestamp = DateTime.now().millisecondsSinceEpoch;
String conversationID = "xxx";// Conversation ID
                              // For one-on-one conversations, the conversationID is the userID of the other party.
                              // For group conversations, the conversationID is the groupID of the group.

ZIMMessageSearchConfig config = ZIMMessageSearchConfig();
config.count = 20;
config.order = ZIMMessageOrder.descending;
config.keywords.add("zego");
config.startTime = startTimestamp;
config.endTime = endTimestamp;

ZIM.getInstance()!.searchLocalMessages("conversationID", ZIMConversationType.peer, config).then((value) {
    // Developers can listen to this callback to get the list of searched messages.
}).catchError((onError){

});
1
Copied!

Search for global local messages

Search results categorized by conversation

After creating a ZIM object and logging in, call the searchGlobalLocalMessages interface, pass in the config parameter to set the search conditions (such as keywords), and globally search for local messages that meet the conditions.

The list of matched messages will be returned and categorized by conversation through the ZIMMessagesGlobalSearchedResult callback interface.

Sample code
// Globally search for local text messages within 7 days that contain "zego".

int startTimestamp = DateTime.now().millisecondsSinceEpoch - (7 * 24 *60 *60 *1000);
int endTimestamp = DateTime.now().millisecondsSinceEpoch;

ZIMMessageSearchConfig config = ZIMMessageSearchConfig();
config.count = 20;  // Number of search results
config.order = ZIMMessageOrder.descending;  // Specify the search order as descending from the last (time) message stored locally
config.keywords.add('zego'); // Set the keyword as "zego", supports up to 5 keywords. When multiple keywords are set, the search results will only show local messages that contain all the keywords at the same time
config.messageTypes.add(ZIMMessageType.text); // Specify the message type as text
config.startTime = startTimestamp; // Start time of the search
config.endTime = endTimestamp;  // End time of the search

ZIM.getInstance()!.searchGlobalLocalMessages(config).then((value) {
    // Developers can listen to this callback to get the list of searched messages.
}).catchError((onError){

});
1
Copied!

Search for conversations Based on Local Messages

After creating a ZIM object and logging in, call the searchLocalConversations interface, pass in the config parameter to set the search conditions related to local messages, and globally search for conversations that meet the conditions.

The list of matched conversations will be returned through the ZIMConversationsSearchedResult callback interface.

Sample code
// Search for local text messages containing the keyword "zego" within the past 7 days to obtain the corresponding conversation list

// Calculate the timestamp of seven days ago
int startTimestamp = DateTime.now().millisecondsSinceEpoch - (7 * 24 *60 *60 *1000);
int endTimestamp = DateTime.now().millisecondsSinceEpoch;

ZIMConversationSearchConfig config = ZIMConversationSearchConfig();
config.totalConversationCount = 20; 
config.conversationMessageCount = 3;  
config.keywords.add('zego'); // Set the keyword as "zego", supports up to 5 keywords. When multiple keywords are set, the search result will only display local messages that contain all the keywords simultaneously
config.messageTypes.add(ZIMMessageType.text); // Specify the message type as text
config.startTime = startTimestamp; // Start time of the search
config.endTime = endTimestamp;  // End time of the search
  
ZIM.getInstance()!.searchLocalConversations(config).then((value) {

}).catchError((onError){

});
1
Copied!

Previous

Set message extension field

Next

Respond to messages with emoticons