Get message history
tip
- Whether users can retrieve group messages sent before they joined depends on the console setting. Turn on Enable pre-join history access by new member under Chat > Chat settings > Group Channels.
- By default, users who are not group members cannot retrieve group history. To allow this, turn off Restrict chat history access to members under Chat > Chat settings > Group Channels on the console.
Get messages with a query
Use BaseChannel.createMessagesQuery() to create a paginated message query, then call loadNextPage() to load messages.
Code example
Dart
import 'package:ai_nexconn_chat_plugin/ai_nexconn_chat_plugin.dart';
final query = BaseChannel.createMessagesQuery(MessagesQueryParams(
channelIdentifier: ChannelIdentifier(
channelType: ChannelType.direct,
channelId: '<target-user-id>',
),
isAscending: false,
policy: MessageOperationPolicy.localRemote,
startTime: 0,
pageSize: 20,
));
await query.loadNextPage((result, error) {
if (error == null) {
print('Loaded ${result?.data.length} messages');
print('Total matched: ${result?.totalCount}');
}
});
MessagesQueryParams
| Parameter | Type | Default | Description |
|---|---|---|---|
channelIdentifier | ChannelIdentifier | Required | Channel type, ID, and optional subchannel ID. |
isAscending | bool | false | Sort order. false loads newer-to-older messages, true loads older-to-newer messages. |
policy | MessageOperationPolicy | local | Load policy: local, remote, or localRemote. |
startTime | int | 0 | Start timestamp in milliseconds. 0 starts from the latest message when isAscending is false. |
pageSize | int | 20 | Messages per page. |
PageResult<Message>
| Property | Type | Description |
|---|---|---|
data | List<Message> | Messages returned in the current page. |
totalCount | int | Total number of messages matched by the query. |
Get a message by ID
Retrieve a single message from the local database by its local ID or server-assigned unique ID.
Dart
await BaseChannel.getMessageById(
GetMessageByIdParams(messageId: messageId),
(message, error) {
if (error == null) {
print('Message: ${message?.messageId}');
}
},
);
Get messages around a timestamp
Retrieve messages before and after a specified timestamp.
Dart
final channel = DirectChannel('<target-user-id>');
await channel.getMessagesAroundTime(
GetMessagesAroundTimeParams(
sentTime: <target-timestamp>,
beforeCount: 10,
afterCount: 10,
),
(messages, error) {
if (error == null) {
print('Messages: ${messages?.length}');
}
},
);
Get local messages by type
info
The Flutter wrapper does not expose a dedicated local-messages-by-type query, and MessagesQueryParams does not include a messageType field. Query messages with BaseChannel.createMessagesQuery() first, then filter the returned Message objects in your app if you only need specific message types.