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(
channel: ChannelIdentifier(
channelType: ChannelType.direct,
channelId: '<target-user-id>',
),
order: TimeOrder.before,
policy: MessageOperationPolicy.localRemote,
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 |
|---|---|---|---|
channel | ChannelIdentifier | Required | Channel type, ID, and optional subchannel ID. |
order | TimeOrder | TimeOrder.before | Direction. before: messages before the last loaded time. after: messages after. |
policy | MessageOperationPolicy | local | Load policy: local, remote, or localRemote. |
messageType | MessageType | unknown | Filter by message type. unknown means all types. |
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
Use BaseChannel.createLocalMessagesByTypeQuery() with a type filter to retrieve local messages by type.