logo
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 ZIMMessagesSearchedCallback callback interface and classified by the corresponding conversation.

Sample code
// Search for local text messages containing "zegocloud" in a single conversation within 7 days.
long startTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
// Calculate the timestamp of seven days ago
startTimestamp = startTimestamp - (7 * 24 * 60 * 60 * 1000);
long endTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
string conversationID = "xxxx"; // 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 = new ZIMMessageSearchConfig();
config.count = 20;  // Number of search results
config.order = ZIMMessageOrder.Descending;  // Specify the search order as querying from the last (time) message stored locally
config.keywords.Add("zegocloud");  // Set the keyword as "zegocloud", supports up to 5 keywords. When multiple keywords are set, the search results 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().SearchLocalMessages(conversationID, ZIMConversationType.Single, config, (string conversationID, ZIMConversationType conversationType,
            List<ZIMMessage> messageList,
            ZIMMessage nextMessage, ZIMError errorInfo) =>
    {
        // Developers can listen for the callback to get the searched message list.
    });
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 ZIMMessagesGlobalSearchedCallback callback interface.

Sample code
// Search for local text messages containing "zegocloud" within 7 days.

long startTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
// Calculate the timestamp of seven days ago
startTimestamp = startTimestamp - (7 * 24 * 60 * 60 * 1000);
long endTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

ZIMMessageSearchConfig config = new ZIMMessageSearchConfig();
config.count = 20;  // Number of search results
config.order = ZIMMessageOrder.Descending;  // Specify the search order as querying from the last (time) message stored locally
config.keywords.Add("zegocloud");  // Set the keyword as "zegocloud", supports up to 5 keywords. When multiple keywords are set, the search results 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, (List<ZIMConversation> conversationList,
        ZIMError errorInfo) =>
    {
        // Developers can listen for the callback to get the searched message list.
    });
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 ZIMConversationsSearchedCallback callback interface.

Sample code
long startTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
// Calculate the timestamp of seven days ago
startTimestamp = startTimestamp - (7 * 24 * 60 * 60 * 1000);
long endTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

// Search for local text messages containing the keyword "zegocloud" within 7 days to obtain the corresponding conversation list
ZIMConversationSearchConfig config = new ZIMConversationSearchConfig();
config.totalConversationCount = 10;
config.conversationMessageCount = 3;
config.nextFlag = 0;
config.keywords.Add("zegocloud");
config.messageTypes.Add(ZIMMessageType.Text);
config.startTime = startTimestamp;
config.endTime = endTimestamp;
ZIM.GetInstance().SearchLocalConversations(conversationSearchConfig, (List<ZIMConversationSearchInfo> conversationSearchInfoList,
        uint nextFlag, ZIMError errorInfo) =>
    {
        // Developers can use this callback to listen for the searched conversation information
    });
1
Copied!

Previous

Set message extension field

Next

Respond to messages with emoticons