Quick Reference for AI Agents & Developers
- Adding a listener to receive real-time message edit events when your app is running
- Calling a method to retrieve missed message edit events when your app was not running
Edit a Message
How do I edit a message? To edit a message, use theeditMessage() method. This method takes an object of the BaseMessage class. Currently, you can only edit TextMessage and CustomMessage. Thus, the BaseMessage object must either be a Text or a Custom Message. For more details on message types, see Message Structure & Hierarchy.
Add/Update Tags
While editing a message, you can update the tags associated with the Message. You can use thesetTags() method to do so. The tags added while editing a message will replace the tags set when the message was sent.
- Java
- Kotlin
editMessage() method and pass the message object to it.
- Java
- Kotlin
onSucess() callback method of the listener. The message object will contain the editedAt field set with the timestamp of the time the message was edited. This will help you identify if the message was edited while iterating through the list of messages. The editedBy field is also set to the UID of the user who edited the message.
By default, CometChat allows certain roles to edit a message.
| User Role | Conversation Type | Edit Capabilities |
|---|---|---|
| Message Sender | One-on-one Conversation | Messages they’ve sent |
| Message Sender | Group Conversation | Messages they’ve sent |
| Group Owner | Group Conversation | All messages in the group |
| Group Moderator | Group Conversation | All messages in the group |
Real-time Message Edit Events
How do I know when someone has edited their message while my app is running? To receive real-time events for messages being edited, override theonMessageEdited() method of the MessageListener class. For more information on message listeners, see Real-Time Listeners.
- Java
- Kotlin
Missed Message Edit Events
How do I know when someone edited their message while my app was not running? When you retrieve the list of previous messages, for the message that was edited, theeditedAt and the editedBy fields will be set. Also, for example, if the total number of messages for a conversation is 100, and the message with message ID 50 was edited, the message with ID 50 will have the editedAt and the editedBy fields set whenever it is pulled from the history. Additionally, the 101st message will be an Action message informing you that the message with ID 50 has been edited..
For the message edited event, in the Action object received, the following fields can help you get the relevant information-
action-editedactionOn- Updated message object with the edited details.actionBy- User object containing the details of the user who has edited the message.actionFor- User/group object having the details of the receiver to which the message was sent.
In order to edit a message, you need to be either the sender of the message or the admin/moderator of the group in which the message was sent.
Best Practices
Preserve Original Message ID
Preserve Original Message ID
Always set the message ID from the original message using
setId() before calling editMessage(). Without the correct ID, the edit operation will fail.Update Tags Strategically
Update Tags Strategically
Handle Edit Permissions
Handle Edit Permissions
Check user roles before allowing edits in your UI. Only message senders, group owners, and group moderators can edit messages to prevent unauthorized modifications.
Troubleshooting
Edit Failed - Permission Denied
Edit Failed - Permission Denied
Symptom:
editMessage() fails with “Permission denied” error.Cause: User attempting to edit a message they didn’t send, or lacking admin/moderator privileges in a group.Solution: Verify the logged-in user is either the message sender or has admin/moderator role in the group. Use message.getSender().getUid() to check message ownership.Edit Failed - Message ID Missing
Edit Failed - Message ID Missing
Symptom:
editMessage() fails with “Invalid message ID” error.Cause: The message ID was not set on the updated message object before calling editMessage().Solution: Always call updatedMessage.setId(originalMessage.getId()) before editing. The ID is required to identify which message to update.Cannot Edit Media Messages
Cannot Edit Media Messages
Symptom: Attempting to edit a media message fails.Cause: CometChat only supports editing
TextMessage and CustomMessage types. Media messages cannot be edited.Solution: Check the message type before allowing edits. Only enable edit functionality for text and custom messages in your UI.