let messageRepository =AmityMessageRepository(client: client)var token: AmityNotificationToken?...funcqueryMessagesExample() {let messagesCollection = messageRepository.getMessages(channelId:"channelId", includingTags: [], excludingTags: [], filterByParentId:false, parentId:nil, reverse:false) token = messagesCollection.observe { collection, change, error infor message in collection.allObjects() {// For example, to handle each message in the list. } }}funcqueryMessagesWithTagsExample() {// Queries messages from tags which are containing "games", but exclude ones containing "staff-only".let messagesCollection = messageRepository.getMessages(channelId:"channelId", includingTags: ["games"], excludingTags: ["staff-only"], filterByParentId:false, parentId:nil, reverse:false) token = messagesCollection.observe { collection, change, error infor message in collection.allObjects() {// For example, to handle each message in the list. } }}funcqueryChildrenMessagesExample(){// A query for children messages belongs to the parentId.// Please note that `parentId` is required, along with `filterByParentId` must be true.let childrenMessagesCollection = messageRepository.getMessages(channelId:"channelId", includingTags: [], excludingTags: [], filterByParentId:true, parentId:"message-123", reverse:false) token = childrenMessagesCollection.observe { collection, change, error infor message in collection.allObjects() {// For example, to handle each message in the list. } }}
This method will return a LiveObject of all messages in the specified channel. You can observe the LiveObject in order to update your view whenever you receive new messages.
Pagination
When querying messages, you cannot set limit, skip, after, first, before, and last parameters. By default, number of items in each page is 20. To handle pagination to load more messages, refer to Pagination in Live Objects.
Get single message for some message
To get a specific message:
funcgetParticularMessageExample() {let messageObject = messageRepository.getMessage("messageId") token = messageObject?.observe { message, error in// Do something with the message }}
Message First Fetch
While the SDK will always return messages in chronological order, developers can ask for the messages to begin from the top of the recyclerview or the bottom of the recyclerview depeding on scrolling direction.
var messagesCollection: AmityCollection<AmityMessage>?var token: AmityNotificationToken?...funcobserveMessageCollection() {// Call this function once to start observing message collection. messagesCollection = messageRepository.getMessages(channelId:"channelId", includingTags: [], excludingTags: [], filterByParentId:false, parentId:nil, reverse:false) token = messagesCollection?.observe { collection, change, error infor message in collection.allObjects() {// For example, to handle each message in the list. } }}funcloadPreviousMessages() {// When necessary load more (older) messages. // If there are any older messages, the observe block in `observeMessageCollection()` will be automatically triggered.
messagesCollection?.previousPage()}
There are other situations where fetching the oldest messages is preferred, for example for archiving purposes or in community forums.
funcloadNextMessage() {// When necessary load more (newer) messages. // If there is any newer messages, the observe block in `observeMessageCollection()` will be automatically triggered.
messagesCollection?.nextPage()}