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 searchLocalMessagesByConversationID 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 "zego" in a single conversation within 7 days.
NSDate *currentDate = [NSDate date];
NSTimeInterval currentTimestamp = [currentDate timeIntervalSince1970];
    
long long startTimestamp = currentTimestamp - (7 * 24 * 60 * 60 * 1000);
    
NSString *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 =  [[ZIMMessageSearchConfig alloc] init];
config.count = 20;  // Number of search results
config.order = ZIMMessageOrderDescending;  // Specify the search order as querying from the last (time) message stored locally
config.keywords = @[@"zego"];  // Set the keyword as "zego", up to 5 keywords are supported. When multiple keywords are set, the search result will only display local messages that contain all the keywords at the same time
config.messageTypes = @[[NSNumber numberWithInt:ZIMMessageTypeText]]; // Specify the message type as text
config.startTime = startTimestamp; // The start time of the search
config.endTime = currentTimestamp; // The end time of the search

    
[[ZIM getInstance] searchLocalMessagesByConversationID:conversationID conversationType:ZIMConversationTypePeer config:config callback:^(NSString * _Nonnull conversationID, ZIMConversationType conversationType, NSArray<ZIMMessage *> * _Nonnull messageList, ZIMMessage * _Nullable nextMessage, ZIMError * _Nonnull errorInfo) {
    // Developers can use this callback to listen for 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 searchGlobalLocalMessagesWithConfig 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
// Globally search for local text messages within 7 days that contain "zego".

NSDate *currentDate = [NSDate date];
NSTimeInterval currentTimestamp = [currentDate timeIntervalSince1970];
    
long long startTimestamp = currentTimestamp - (7 * 24 * 60 * 60 * 1000);

ZIMMessageSearchConfig *config = [[ZIMMessageSearchConfig alloc] init];
config.count = 20;  // Number of search results
config.order = ZIMMessageOrderDescending;  // Specify the search order as querying from the last (time) message stored locally
config.keywords = @[@"zego"];  // Set the keyword as "zego", up to 5 keywords are supported. When multiple keywords are set, the search result will only display local messages that contain all the keywords at the same time
config.messageTypes = @[[NSNumber numberWithInt:ZIMMessageTypeText]]; // Specify the message type as text
config.startTime = startTimestamp; // The start time of the search
config.endTime = currentTimestamp; // The end time of the search
[[ZIM getInstance] searchGlobalLocalMessagesWithConfig:config callback:^(NSArray<ZIMMessage *> * _Nonnull messageList, ZIMMessage * _Nullable nextMessage, ZIMError * _Nonnull errorInfo) {
    // Developers can use this callback to listen for the searched message list.
}];
1
Copied!

Search for conversations Based on Local Messages

After creating a ZIM object and logging in, call the searchLocalConversationsWithConfig 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
// Search for local text messages within the last 7 days that contain the keyword "zego" to get the corresponding conversation list

NSDate *currentDate = [NSDate date];
NSTimeInterval currentTimestamp = [currentDate timeIntervalSince1970];

long long startTimestamp = currentTimestamp - (7 * 24 * 60 * 60 * 1000);
ZIMConversationSearchConfig *config = [[ZIMConversationSearchConfig alloc] init];
config.totalConversationCount = 10;
config.conversationMessageCount = 3;
config.nextFlag = 0;
config.keywords = @[@"zego"];  // Set the keyword as "zego", supports up to 5 keywords
config.messageTypes = @[[NSNumber numberWithInt:ZIMMessageTypeText]]; // Specify the message type as text
config.startTime = startTimestamp;
config.endTime = currentTimestamp;

[[ZIM getInstance] searchLocalConversationsWithConfig:config callback:^(NSArray<ZIMConversationSearchInfo *> * _Nonnull conversationSearchInfoList, unsigned int nextFlag, ZIMError * _Nonnull 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