For the feature effect, see Android SDK Tuikit.
Upgrade to the Flagship Edition package by referring to Purchase Guide.
You can call the searchFriends API to search for local user profiles. The API parameter searchParams supports the search fields nickName
, userID
, and remark
. You can use this API to implement the friend search feature, such as searching for friends' nicknames.
V2TimFriendSearchParam searchParam = new V2TimFriendSearchParam(
keywordList: ["Your keyword"],
isSearchUserID: true, // Whether to include `userId` in the search
isSearchNickName: true, // Whether to include `nickName` in the search
isSearchRemark: true); // Whether to include `remark` in the search
var res = await TencentImSDKPlugin.v2TIMManager
.getFriendshipManager()
.searchFriends(searchParam: searchParam);
You can call the searchGroups API to search for the profiles of local groups.
You can call the searchGroupMembers API to search for the profiles of local group members. There are two cases, depending on whether the value of groupIDList
in V2TimGroupMemberSearchParam is null
:
You can enter keywords in the search box to call searchLocalMessages to search for local messages. There are two cases, depending on whether the value of conversationID in V2TIMMessageSearchParam is null
:
Displaying recent active conversations
The list of the recent conversations to which the messages found belong is implemented as follows:
null
, indicating the messages of all conversations are searched;pageIndex is set to 0
, indicating data on page 0 of the conversations to which the messages found belong; pageSize indicates the number of recent conversations to be returned.totalCount
indicates the total number of conversations to which the matched messages belong; messageSearchResultItems indicates the information of the recent conversations (conversation quantity specified by pageSize). In V2TimMessageSearchResultItem, conversationID
indicates the conversation ID, messageCount
indicates the total number of messages found in the current conversation, and messageList
indicates the list of messages found. When searching for messages in all conversations
, messageList
has two cases:messageList
is empty. You can display related chat records (quantity specified by messageCount
) on the UI.messageList
is the matched message. You can display the message content on the UI and highlight the search keyword, such as test in the above figures.a specified conversation
, messageList
lists all eligible messages in the specified conversation.Sample
V2TimMessageSearchParam searchParam = V2TimMessageSearchParam(
keywordList: [keyword],
type:1, // Corresponds to the V2TIMKeywordListMatchType.KEYWORD_LIST_MATCH_TYPE_AND SDK-layer processing. Indicates the OR or AND relationship.
pageSize: 50,
pageIndex: 0,
conversationID: null, // Setting `conversationID` to `null` is to search for messages in all conversations.
);
V2TimValueCallback<V2TimMessageSearchResult> res = await TencentImSDKPlugin.v2TIMManager
.getMessageManager()
.searchLocalMessages(searchParam: searchParam);
Displaying the list of conversations to which the messages found belong
For example, you can click More Chat History in Figure 1 to redirect to the list of conversations to which the messages found belong, as shown in Figure 2. The search parameters and results are similar to those in the preceding scenario. To avoid memory ballooning, it is strongly recommended that you load the conversation list with pagination. For example, to display the search results by 10 conversations per page you can set the search parameter API V2TIMMessageSearchParam as follows:
pageSize
to 10
and pageIndex
to 0
, and call searchLocalMessages. Then you can get the total number of conversations from totalCount
in the callback.pageIndex
(pageIndex
< totalPage
) to return the subsequent page number.Sample
......
static int totalCount = 0;
int index = 0;
// Calculate the total number of pages, given that 10 messages are displayed per page
double totalPage = (totalCount % 10 == 0) ? (totalCount / 10) : (totalCount / 10 + 1);
......
searchLocaltMessage(int index) async {
if (index >= totalPage) {
return;
}
V2TimMessageSearchParam searchParam = V2TimMessageSearchParam(
keywordList: ["Your keyword"],
type:
1, // Corresponds to the V2TIMKeywordListMatchType.KEYWORD_LIST_MATCH_TYPE_AND SDK-layer processing. Indicates the OR or AND relationship.
pageSize: 10,
pageIndex: index,
conversationID: null, // Setting `conversationID` to `null` is to search for messages in all conversations.
);
V2TimValueCallback<V2TimMessageSearchResult> res = await TencentImSDKPlugin
.v2TIMManager
.getMessageManager()
.searchLocalMessages(searchParam: searchParam);
// Total number of messages matching the conversation
totalCount = res.data?.totalCount ?? 0;
List<V2TimMessageSearchResultItem> list =
res.data!.messageSearchResultItems!;
totalPage =
(totalCount % 10 == 0) ? (totalCount / 10) : (totalCount / 10 + 1);
for (V2TimMessageSearchResultItem resultItem in list) {
// Conversation ID
String conversationID = resultItem.conversationID!;
// Total number of messages matching the conversation
int totalMessageCount = resultItem.messageCount!;
// List of messages. If `totalMessageCount` is greater than 1, the list is empty. If `totalMessageCount` is equal to 1, the list contains the current message.
List<V2TimMessage> v2TIMMessageList = resultItem.messageList!;
// ...
}
}
void loadMore() {
index = index++;
searchLocaltMessage(index);
}
Searching for messages in a specified conversation
The following introduces how to search for messages in a specified conversation. To avoid memory ballooning, it is strongly recommended that you load the message list with pagination. The implementation method is as follows:
conversationID
to the ID of the conversation to search, and set pageIndex
and pageSize
by referring to the settings in the preceding calculation mode.totalCount
indicates the total number of messages matched in the conversation, and messageSearchResultItems lists only the results in the conversation. In V2TIMMessageSearchResultItem, messageCount
is the number of messages on the current page, and messageList
is the list of messages on the current page.Sample
......
static int totalCount = 0;
int index = 0;
// Calculate the total number of pages, given that 10 messages are displayed per page
double totalPage = (totalCount % 10 == 0) ? (totalCount / 10) : (totalCount / 10 + 1);
......
searchLocaltMessage(int index) async {
if (index >= totalPage) {
return;
}
V2TimMessageSearchParam searchParam = V2TimMessageSearchParam(
keywordList: ["Your keyword"],
type:
1, // Corresponds to the V2TIMKeywordListMatchType.KEYWORD_LIST_MATCH_TYPE_AND SDK-layer processing. Indicates the OR or AND relationship.
pageSize: 10,
pageIndex: index,
conversationID: "ID of the conversation for message searching", // Specify the conversation ID
);
V2TimValueCallback<V2TimMessageSearchResult> res = await TencentImSDKPlugin
.v2TIMManager
.getMessageManager()
.searchLocalMessages(searchParam: searchParam);
// Total number of messages matching the conversation
totalCount = res.data?.totalCount ?? 0;
List<V2TimMessageSearchResultItem> list =
res.data!.messageSearchResultItems!;
totalPage =
(totalCount % 10 == 0) ? (totalCount / 10) : (totalCount / 10 + 1);
for (V2TimMessageSearchResultItem resultItem in list) {
// Conversation ID
String conversationID = resultItem.conversationID!;
// Total number of messages matching the conversation
int totalMessageCount = resultItem.messageCount!;
// List of messages. If `totalMessageCount` is greater than 1, the list is empty. If `totalMessageCount` is equal to 1, the list contains the current message.
List<V2TimMessage> v2TIMMessageList = resultItem.messageList!;
// ...
}
}
void loadMore() {
index = index++;
searchLocaltMessage(index);
}
Use the createCustomMessage({required String data,String desc,String extension}) API to create and send a search request. In the request, you need to specify the text to search in the desc
parameter. Custom messages created via the createCustomMessage (required String data) API cannot be searched because the binary data stream passed in by parameters is saved locally.
If you configure the offline push feature and the description
parameter, custom messages will also be pushed offline, and the content specified in the description
parameter will be displayed in the notification bar. If offline push is not needed, disable it using disablePush in V2TIMOfflinePushInfo of the sendMessage API. If you do not want to display the searched text in the push notification bar, set other push content using desc in V2TIMOfflinePushInfo.
Rich media messages include file, image, voice, and video messages.
For file messages, the screen usually displays the filename. Therefore, you can set the fileName
parameter as the searched content when creating messages. If fileName
is not set, the system gets the filename from filePath
and saves it to the local storage and the server. Note that file messages cannot be searched in the web SDK. For image, voice, and video messages, the screen usually displays the thumbnail or duration. In this case, you can specify the message type to search by type, but cannot search by keywords.
Was this page helpful?