AI Integration Quick Reference
AI Integration Quick Reference
Saving a message with the SDK requires the Save Message feature to be enabled for your app. You can check its availability at runtime using the feature flag.
Save a Message
To save a message, use thesaveMessage method and pass the ID of the message. On success, the callback returns the updated BaseMessage with its savedAt attribute set.
- Java
- Kotlin
getSavedAt() rather than failing.
Unlike pinning, saving has no role restrictions — every user can save any message they have access to. Deleted messages cannot be saved.
Unsave a Message
To remove a message from the user’s saved list, use theunsaveMessage method. On success, the callback returns the updated BaseMessage with its savedAt attribute cleared.
- Java
- Kotlin
Fetch Saved Messages
To fetch all messages the logged-in user has saved, create aMessagesRequest with the setSaved(true) filter of the MessagesRequestBuilder. Because saved messages are user-level and span conversations, you must not set a UID or GUID. The returned list is sorted by the time of saving, most recently saved first.
- Java
- Kotlin
fetchNext() until a call returns an empty list; the cursor rides on savedAt instead of sentAt, and the SDK swaps it for you.
If the user loses access to a conversation (for example, they are removed from a group), messages saved from it are cleaned up and no longer returned.
Check if a Message is Saved
Every fetched message carries the logged-in user’s save state on theBaseMessage itself. These values are per-user: the same message shows different values to different users.
- Java
- Kotlin
Real-time Save Events
Because saving is private, save events are never delivered to other participants. Register aMessageListener and override the save callbacks; each event delivers the full updated BaseMessage.
Your own save or unsave fires onMessageSaved / onMessageUnsaved on the device that performed it, just before the saveMessage() / unsaveMessage() success callback, so update your UI in one of the two, not both. These callbacks also fire on the user’s other devices when a save frame arrives there.
- Java
- Kotlin
CometChat.removeMessageListener(listenerID).
Save Limit
A user can save a capped number of messages, configurable per app. Read the cap rather than hard-coding it — it is tenant-overridable and will drift.- Java
- Kotlin
Settings.LIMIT_UNSPECIFIED (-1) when the app settings carry no value or when it is called before init() completes — show generic copy in that case rather than guessing a number. Unlike pinning, saving has no separate system cap.
When the limit is breached, the SDK surfaces the server error through onError, and the applicable limit is carried in the exception’s errorParams:
- Java
- Kotlin
Error Handling
Feature Availability
Check whether the Save Message feature is enabled for your app before showing save actions in your UI. The method is synchronous and safe to call from the UI layer.- Java
- Kotlin
Offline Behaviour
With offline support enabled,fetchNext() on a setSaved(true) request delivers a cached page first (onCachedMessages, even when empty) and then the server list (onServerMessages); offline, it ends with onError with code ERROR_INTERNET_UNAVAILABLE after the cached page. onCachedMessages holds the user’s cached saved messages across all conversations. The saved list is answered from its own store, so other MessagesRequestBuilder filters are ignored, as on the server.
Next Steps
Pin A Message
Highlight a message for everyone in the conversation
Pin A Conversation
Pin a conversation to the top of the list
All Real Time Listeners
Every listener the SDK exposes, in one place
Additional Message Filtering
Filter messages by pinned, saved, type, tags and more