|
11 | 11 | - `EMChatManager.searchMsgFromDB`:根据关键字搜索会话消息。 |
12 | 12 | - `EMChatManager#loadMessagesWithKeyword`:根据搜索范围搜索所有会话中的消息。 |
13 | 13 | - `EMConversation#loadMessagesWithKeyword`:根据搜索范围搜索当前会话中的消息。 |
| 14 | +- `EMChatManager#searchMsgsByOptions`:根据单个或多个消息类型,搜索本地数据库中所有会话的消息。 |
| 15 | +- `EMConversation#searchMsgsByOptions` 根据单个或多个消息类型,搜索本地数据库中单个会话的消息。 |
14 | 16 |
|
15 | 17 | ## 前提条件 |
16 | 18 |
|
|
90 | 92 | debugPrint("loadMessagesWithKeyword error: ${e.code}, ${e.description}"); |
91 | 93 | } |
92 | 94 |
|
93 | | -``` |
| 95 | +``` |
| 96 | + |
| 97 | +### 根据消息类型搜索所有会话中的消息 |
| 98 | + |
| 99 | +你可以调用 `EMChatManager#searchMsgsByOptions` 方法除了设置消息时间戳、消息数量、发送方、搜索方向等条件搜索当前会话中的消息,你还可以设置单个或多个消息类型搜索本地数据库中所有会话的消息。 |
| 100 | + |
| 101 | +:::tip |
| 102 | +若使用该功能,需将 SDK 升级至 V4.8.1 或以上版本。 |
| 103 | +::: |
| 104 | + |
| 105 | +```dart |
| 106 | +// from:会话中发送方的用户 ID。若传空字符串,搜索对发送方不限制。 |
| 107 | +// count:要查询的消息条数。取值范围为 [1,400]。 |
| 108 | +try { |
| 109 | + const searchOptions = MessageSearchOptions( |
| 110 | + types: [MessageType.TXT, MessageType.IMAGE], |
| 111 | + from: fromUser, |
| 112 | + ts: startTime, |
| 113 | + direction: EMSearchDirection.Up, |
| 114 | + count: 50 |
| 115 | + ); |
| 116 | + List<EMMessage> msgs = |
| 117 | + await EMClient.getInstance.chatManager.searchMsgsByOptions( |
| 118 | + searchOptions, |
| 119 | + ); |
| 120 | +} on EMError catch (e) { |
| 121 | + debugPrint("error code: ${e.code}, desc: ${e.description}"); |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +### 根据消息类型搜索当前会话中的消息 |
| 126 | + |
| 127 | +你可以调用 `EMConversation#searchMsgsByOptions` 方法除了设置消息时间戳、消息数量、发送方、搜索方向等条件搜索当前会话中的消息,你还可以设置单个或多个消息类型搜索本地数据库中单个会话的消息。 |
| 128 | + |
| 129 | +:::tip |
| 130 | +若使用该功能,需将 SDK 升级至 V4.8.1 或以上版本。 |
| 131 | +::: |
| 132 | + |
| 133 | +```dart |
| 134 | +// from:当前会话中发送方的用户 ID。若传空字符串,搜索对发送方不限制。 |
| 135 | +// count:要查询的消息条数。取值范围为 [1,400]。 |
| 136 | + try { |
| 137 | + const searchOptions = MessageSearchOptions( |
| 138 | + types: [MessageType.TXT, MessageType.IMAGE], |
| 139 | + from: fromUser, |
| 140 | + ts: startTime, |
| 141 | + direction: EMSearchDirection.Up, |
| 142 | + count: 50); |
| 143 | + conversation.searchMsgsByOptions( |
| 144 | + searchOptions, |
| 145 | + ); |
| 146 | + } on EMError catch (e) { |
| 147 | + debugPrint("error code: ${e.code}, desc: ${e.description}"); |
| 148 | + } |
| 149 | +``` |
0 commit comments