On success, sendMediaMessage() returns a MediaMessage object.
Multiple Attachments
Starting version 3.0.9, the SDK supports sending multiple attachments in a single media message.
Upload Multiple Files
Java (User)
Kotlin (User)
Java (Group)
Kotlin (Group)
String receiverId = "cometchat-uid-1";List<File> files = new ArrayList<File>();files.add(new File("/storage/emulated/0/Download/1.jpg"));files.add(new File("/storage/emulated/0/Download/2.jpg"));files.add(new File("/storage/emulated/0/Download/3.jpg"));MediaMessage mediaMessage = new MediaMessage(receiverId, files, fileType, CometChatConstants.RECEIVER_TYPE_USER);CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener<MediaMessage>() { @Override public void onSuccess(MediaMessage mediaMessage) { Log.d(TAG, "Message Sent Successfully") } @Override public void onError(CometChatException e) { Log.d(TAG, "Message Sending Failed with exception : " + e.getMessage()); }});
val receiverId = "cometchat-uid-1"val files: MutableList<File> = ArrayList()files.add(File("/storage/emulated/0/Download/1.jpg"))files.add(File("/storage/emulated/0/Download/2.jpg"))files.add(File("/storage/emulated/0/Download/3.jpg"))val mediaMessage = MediaMessage(receiverId, files, fileType, CometChatConstants.RECEIVER_TYPE_USER)CometChat.sendMediaMessage(mediaMessage, object : CallbackListener<MediaMessage?>() { override fun onSuccess(mediaMessage: MediaMessage?) { Log.d(TAG, "Message Sent Successfully") } override fun onError(e: CometChatException) { Log.d(TAG, "Message Sending Failed with exception : " + e.message) }})
String groupId = "cometchat-guid-1";List<File> files = new ArrayList<File>();files.add(new File("/storage/emulated/0/Download/1.jpg"));files.add(new File("/storage/emulated/0/Download/2.jpg"));files.add(new File("/storage/emulated/0/Download/3.jpg"));MediaMessage mediaMessage = new MediaMessage(groupId, files, fileType, CometChatConstants.RECEIVER_TYPE_GROUP);CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener<MediaMessage>() { @Override public void onSuccess(MediaMessage mediaMessage) { Log.d(TAG, "Message Sent Successfully") } @Override public void onError(CometChatException e) { Log.d(TAG, "Message Sending Failed with exception : " + e.getMessage()); }});
val groupId = "cometchat-guid-1"val files: MutableList<File> = ArrayList()files.add(File("/storage/emulated/0/Download/1.jpg"))files.add(File("/storage/emulated/0/Download/2.jpg"))files.add(File("/storage/emulated/0/Download/3.jpg"))val mediaMessage = MediaMessage(groupId, files, fileType, CometChatConstants.RECEIVER_TYPE_GROUP)CometChat.sendMediaMessage(mediaMessage, object : CallbackListener<MediaMessage?>() { override fun onSuccess(mediaMessage: MediaMessage?) { Log.d(TAG, "Message Sent Successfully") } override fun onError(e: CometChatException) { Log.d(TAG, "Message Sending Failed with exception : " + e.message) }})
The MediaMessage class constructor takes the following parameters:
Parameter
Description
Required
receiverId
The UID or GUID of the recipient
Yes
files
A List<File> of files to upload
Yes
messageType
MESSAGE_TYPE_IMAGE, MESSAGE_TYPE_VIDEO, MESSAGE_TYPE_AUDIO, or MESSAGE_TYPE_FILE
Yes
receiverType
RECEIVER_TYPE_USER or RECEIVER_TYPE_GROUP
Yes
Send Multiple URLs
Java (User)
Kotlin (User)
Java (Group)
Kotlin (Group)
String receiverId = "cometchat-uid-1";MediaMessage mediaMessage = new MediaMessage(receiverId, CometChatConstants.MESSAGE_TYPE_IMAGE, CometChatConstants.RECEIVER_TYPE_USER);List<Attachment> attachments = new ArrayList<>();for (int i = 0; i < 5; i++) { Attachment attachment = new Attachment(); attachment.setFileName("test"); attachment.setFileExtension("png"); attachment.setFileUrl("https://pngimg.com/uploads/mario/mario_PNG125.png"); attachment.setFileMimeType("image/png"); attachments.add(attachment);}mediaMessage.setAttachments(attachments);CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener<MediaMessage>() { @Override public void onSuccess(MediaMessage mediaMessage) { Log.d(TAG, "Message Sent Successfully") } @Override public void onError(CometChatException e) { Log.d(TAG, "Message Sending Failed with exception : " + e.getMessage()); }});
val receiverId = "cometchat-uid-1"val mediaMessage = MediaMessage(receiverId, CometChatConstants.MESSAGE_TYPE_IMAGE, CometChatConstants.RECEIVER_TYPE_USER)val attachments: MutableList<Attachment> = ArrayList()for (i in 0..4) { val attachment = Attachment() attachment.fileName = "test" attachment.fileExtension = "png" attachment.fileUrl = "https://pngimg.com/uploads/mario/mario_PNG125.png" attachment.fileMimeType = "image/png" attachments.add(attachment)}mediaMessage.attachments = attachmentsCometChat.sendMediaMessage(mediaMessage, object : CallbackListener<MediaMessage?>() { override fun onSuccess(mediaMessage: MediaMessage?) { Log.d(TAG, "Message Sent Successfully") } override fun onError(e: CometChatException) { Log.d(TAG, "Message Sending Failed with exception : " + e.message) }})
String groupId = "cometchat-guid-1";MediaMessage mediaMessage = new MediaMessage(groupId, CometChatConstants.MESSAGE_TYPE_IMAGE, CometChatConstants.RECEIVER_TYPE_GROUP);List<Attachment> attachments = new ArrayList<>();for (int i = 0; i < 5; i++) { Attachment attachment = new Attachment(); attachment.setFileName("test"); attachment.setFileExtension("png"); attachment.setFileUrl("https://pngimg.com/uploads/mario/mario_PNG125.png"); attachment.setFileMimeType("image/png"); attachments.add(attachment);}mediaMessage.setAttachments(attachments);CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener<MediaMessage>() { @Override public void onSuccess(MediaMessage mediaMessage) { Log.d(TAG, "Message Sent Successfully") } @Override public void onError(CometChatException e) { Log.d(TAG, "Message Sending Failed with exception : " + e.getMessage()); }});
val groupId = "cometchat-guid-1"val mediaMessage = MediaMessage(groupId, CometChatConstants.MESSAGE_TYPE_IMAGE, CometChatConstants.RECEIVER_TYPE_GROUP)val attachments: MutableList<Attachment> = ArrayList()for (i in 0..4) { val attachment = Attachment() attachment.fileName = "test" attachment.fileExtension = "png" attachment.fileUrl = "https://pngimg.com/uploads/mario/mario_PNG125.png" attachment.fileMimeType = "image/png" attachments.add(attachment)}mediaMessage.attachments = attachmentsCometChat.sendMediaMessage(mediaMessage, object : CallbackListener<MediaMessage?>() { override fun onSuccess(mediaMessage: MediaMessage?) { Log.d(TAG, "Message Sent Successfully") } override fun onError(e: CometChatException) { Log.d(TAG, "Message Sending Failed with exception : " + e.message) }})
When a media message is sent successfully, the response will include a MediaMessage object which includes all information related to the sent message.You can use the setMetadata(), setCaption() & setTags() methods to add metadata, caption and tags in the same way as a single attachment.
Custom Message
Send custom data that doesn’t fit text or media using sendCustomMessage().
Java
Kotlin
CustomMessage customMessage = new CustomMessage(receiverId, receiverType, customType, customData)
val customMessage = CustomMessage(receiverId, receiverType, customType, customData)
Parameter
Description
Required
receiverId
UID of the user or GUID of the group
Yes
receiverType
RECEIVER_TYPE_USER or RECEIVER_TYPE_GROUP
Yes
customType
Developer-defined type string (e.g., "LOCATION", "POLL")
Yes
customData
The payload as a JSONObject
Yes
You can also set a subtype using setSubtype() to further classify the custom message.
Add Tags
Java
Kotlin
List<String> tags = new ArrayList<>();tags.add("pinned");customMessage.setTags(tags);
val tags: MutableList<String> = ArrayList()tags.add("pinned")customMessage.setTags(tags)
Once the message object is ready, call sendCustomMessage().
Java (User)
Kotlin (User)
Java (Group)
Kotlin (Group)
private String UID = "UID";private String customType = "LOCATION";JSONObject customData = new JSONObject();customData.put("latitude", "19.0760");customData.put("longitude", "72.8777");CustomMessage customMessage = new CustomMessage(UID, CometChatConstants.RECEIVER_TYPE_USER, customType, customData);CometChat.sendCustomMessage(customMessage, new CometChat.CallbackListener<CustomMessage>() { @Override public void onSuccess(CustomMessage customMessage) { Log.d(TAG, customMessage.toString()); } @Override public void onError(CometChatException e) { Log.d(TAG, e.getMessage()); }});
private val UID = "UID"private val customType = "LOCATION"val customData = JSONObject()customData.put("latitude", "19.0760")customData.put("longitude", "72.8777")val customMessage = CustomMessage(UID, CometChatConstants.RECEIVER_TYPE_USER, customType, customData)CometChat.sendCustomMessage(customMessage, object : CallbackListener<CustomMessage>() { override fun onSuccess(customMessage: CustomMessage) { Log.d(TAG, customMessage.toString()) } override fun onError(e: CometChatException) { Log.d(TAG, e.message!!) }})
private String GUID = "GUID";private String customType = "LOCATION";JSONObject customData = new JSONObject();customData.put("latitude", "19.0760");customData.put("longitude", "72.8777");CustomMessage customMessage = new CustomMessage(GUID, CometChatConstants.RECEIVER_TYPE_GROUP, customType, customData);CometChat.sendCustomMessage(customMessage, new CometChat.CallbackListener<CustomMessage>() { @Override public void onSuccess(CustomMessage customMessage) { Log.d(TAG, customMessage.toString()); } @Override public void onError(CometChatException e) { Log.d(TAG, e.getMessage()); }});
private val GUID = "GUID"private val customType = "LOCATION"val customData = JSONObject()customData.put("latitude", "19.0760")customData.put("longitude", "72.8777")val customMessage = CustomMessage(GUID, CometChatConstants.RECEIVER_TYPE_GROUP, customType, customData)CometChat.sendCustomMessage(customMessage, object : CallbackListener<CustomMessage>() { override fun onSuccess(customMessage: CustomMessage) { Log.d(TAG, customMessage.toString()) } override fun onError(e: CometChatException) { Log.d(TAG, e.message!!) }})
On success, sendCustomMessage() returns a CustomMessage object.
Control Conversation Update
By default, a custom message updates the conversation’s last message. To prevent this:
Java (User)
Kotlin (User)
Java (Group)
Kotlin (Group)
private String UID = "UID";private String customType = "LOCATION";JSONObject customData = new JSONObject();customData.put("latitude", "19.0760");customData.put("longitude", "72.8777");CustomMessage customMessage = new CustomMessage(UID, CometChatConstants.RECEIVER_TYPE_USER, customType, customData);customMessage.shouldUpdateConversation(false);CometChat.sendCustomMessage(customMessage, new CometChat.CallbackListener<CustomMessage>() { @Override public void onSuccess(CustomMessage customMessage) { Log.d(TAG, customMessage.toString()); } @Override public void onError(CometChatException e) { Log.d(TAG, e.getMessage()); }});
private val UID = "UID"private val customType = "LOCATION"val customData = JSONObject()customData.put("latitude", "19.0760")customData.put("longitude", "72.8777")val customMessage = CustomMessage(UID, CometChatConstants.RECEIVER_TYPE_USER, customType, customData)customMessage.shouldUpdateConversation(false)CometChat.sendCustomMessage(customMessage, object : CallbackListener<CustomMessage>() { override fun onSuccess(customMessage: CustomMessage) { Log.d(TAG, customMessage.toString()) } override fun onError(e: CometChatException) { Log.d(TAG, e.message!!) }})
private String GUID = "GUID";private String customType = "LOCATION";JSONObject customData = new JSONObject();customData.put("latitude", "19.0760");customData.put("longitude", "72.8777");CustomMessage customMessage = new CustomMessage(GUID, CometChatConstants.RECEIVER_TYPE_GROUP, customType, customData);customMessage.shouldUpdateConversation(false);CometChat.sendCustomMessage(customMessage, new CometChat.CallbackListener<CustomMessage>() { @Override public void onSuccess(CustomMessage customMessage) { Log.d(TAG, customMessage.toString()); } @Override public void onError(CometChatException e) { Log.d(TAG, e.getMessage()); }});
private val GUID = "GUID"private val customType = "LOCATION"val customData = JSONObject()customData.put("latitude", "19.0760")customData.put("longitude", "72.8777")val customMessage = CustomMessage(GUID, CometChatConstants.RECEIVER_TYPE_GROUP, customType, customData)customMessage.shouldUpdateConversation(false)CometChat.sendCustomMessage(customMessage, object : CallbackListener<CustomMessage>() { override fun onSuccess(customMessage: CustomMessage) { Log.d(TAG, customMessage.toString()) } override fun onError(e: CometChatException) { Log.d(TAG, e.message!!) }})
Control Notifications
By default, custom messages don’t trigger notifications (push, email, or SMS). To enable them, call shouldSendNotification(true) before sending. See Custom message templates for how notifications are handled for custom messages.
Java
Kotlin
// Enable notification sending for this custom messagecustomMessage.shouldSendNotification(true);
// Enable notification sending for this custom messagecustomMessage.shouldSendNotification(true)
Custom Notification Text
Set custom text for push, email, and SMS notifications. Notifications use this text only when they’re enabled for the message (see Control Notifications). It is also shown as the conversation-list preview.
Java (User)
Kotlin (User)
Java (Group)
Kotlin (Group)
private String UID = "UID";private String customType = "LOCATION";JSONObject customData = new JSONObject();customData.put("latitude", "19.0760");customData.put("longitude", "72.8777");CustomMessage customMessage = new CustomMessage(UID, CometChatConstants.RECEIVER_TYPE_USER, customType, customData);customMessage.setConversationText("Custom Notification Body");customMessage.shouldSendNotification(true);CometChat.sendCustomMessage(customMessage, new CometChat.CallbackListener<CustomMessage>() { @Override public void onSuccess(CustomMessage customMessage) { Log.d(TAG, customMessage.toString()); } @Override public void onError(CometChatException e) { Log.d(TAG, e.getMessage()); }});
It is also possible to send interactive messages from CometChat, to know more click here
Next Steps
Receive Messages
Listen for incoming messages in real time
Edit Message
Modify sent messages after delivery
Delete Message
Remove messages from conversations
Interactive Messages
Send messages with embedded forms and buttons
Message Payload Structure
BaseMessage Object
The BaseMessage object returned by SDK methods contains the following fields. TextMessage, MediaMessage, and CustomMessage extend this base structure with additional type-specific fields.