Delete a Message
While [deleting a message]./messaging-delete-message#delete-a-message) is straightforward, receiving events for deleted messages with CometChat has two parts:
- Adding a listener to receive real-time message deletes when your app is running.
- Calling a method to retrieve missed message delete events-me when your app was not running.
Delete a Message
In other words, as a sender, how do I delete a message?
In case you have to delete a message, you can use the deleteMessage()
method. This method takes the message ID of the message to be deleted.
- Java
- Kotlin
private int messageId=1234;
CometChat.deleteMessage(messageId, new CometChat.CallbackListener<BaseMessage>() {
@Override
public void onSuccess(BaseMessage message) {
Log.d(TAG, "Message deleted successfully at : " + message.getDeletedAt());
}
@Override
public void onError(CometChatException e) {
Log.d(TAG, e.getMessage());
}
});
private val messageId = 1234
CometChat.deleteMessage(messageId,object : CometChat.CallbackListener<BaseMessage>(){
override fun onSuccess(message: BaseMessage) {
Log.d(TAG, "deleteMessage onSuccess : " + message.deletedAt)
}
override fun onError(e: CometChatException) {
Log.d(TAG, "deleteMessage onError : "+e.message)
}
})
Once the message is deleted, In the onSuccess()
callback, you get an object of the BaseMessage
class, with the deletedAt
field set with the timestamp of the time the message was deleted. Also, the deletedBy
field is set. These two fields can be used to identify if the message is deleted while iterating through a list of messages.
By default, CometChat allows certain roles to delete a message.
User Role | Conversation Type | Deletion Capabilities |
---|---|---|
Message Sender | One-on-one Conversation | Messages they've sent |
Message Sender | Group Conversation | Messages they've sent |
Group Admin | Group Conversation | All messages in the group |
Group Moderator | Group Conversation | All messages in the group |
Real-time Message Delete Events
In other words, as a recipient, how do I know when someone deletes a message when my app is running?
In order to receive real-time events for a message being deleted, you need to override the onMessageDeleted()
method of the MessageListener
class.
- Java
- Kotlin
CometChat.addMessageListener(listenerID, new CometChat.MessageListener() {
@Override
public void onMessageDeleted(BaseMessage message) {
Log.d(TAG, "Message Edited");
}
});
CometChat.addMessageListener(listenerID, object : CometChat.MessageListener() {
override fun onMessageDeleted(message: BaseMessage?) {
Log.d(TAG, "Message Edited")
}
})
Missed Message Delete Events
In other words, as a recipient, how do I know if someone deleted a message when my app was not running?
When you retrieve the list of previous messages, for the messages that were deleted, the deletedAt
and the deletedBy
fields will be set. Also, for example, if the total number of messages for a conversation are 100, and the message with message ID 50 was deleted. Now the message with ID 50 will have the deletedAt
and the deletedBy
fields set whenever it is pulled from the history. Also, the 101st message will be an Action
message informing you that the message with ID 50 has been deleted.
For the message deleted event, in the Action
object received, the following fields can help you get the relevant information-
action
-deleted
actionOn
- Updated message object which was deleted.actionBy
- User object containing the details of the user who has deleted the message.actionFor
- User/group object having the details of the receiver to which the message was sent.
In order to delete 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.