Send a Message
Using CometChat, you can send three types of messages:
- A Text Message, the most common and standard message type.
- A Media Message, for sending photos, videos and files.
- A Custom Message, for sending completely custom data using JSON structures.
You can also send metadata along with a text or media message. Think, for example, if you'd want to share the user's location with every message, you can use the metadata field.
Text Message
In other words, as a sender, how do I send a text message?
To send a text message to a single user or group, you need to use the sendMessage()
method and pass a TextMessage
object to it.
- Java(User)
- Kotlin(User)
- Java(Group)
- Kotlin(Group)
private String receiverID = "UID";
private String messageText = "Hello world!";
private String receiverType = CometChatConstants.RECEIVER_TYPE_USER;
TextMessage textMessage = new TextMessage(receiverID, messageText, receiverType);
CometChat.sendMessage(textMessage, new CometChat.CallbackListener <TextMessage> () {
@Override
public void onSuccess(TextMessage textMessage) {
Log.d(TAG, "Message sent successfully: " + textMessage.toString());
}
@Override
public void onError(CometChatException e) {
Log.d(TAG, "Message sending failed with exception: " + e.getMessage());
}
});
val receiverID:String="UID"
val messageText:String="Hello world!"
val receiverType:String=CometChatConstants.RECEIVER_TYPE_USER
val textMessage = TextMessage(receiverID, messageText,receiverType)
CometChat.sendMessage(textMessage, object : CometChat.CallbackListener<TextMessage>() {
override fun onSuccess(p0: TextMessage?) {
Log.d(TAG, "Message sent successfully: " + p0?.toString())
}
override fun onError(p0: CometChatException?) {
Log.d(TAG, "Message sending failed with exception: " + p0?.message) }
})
private String receiverID = "GUID";
private String messageText = "Hello world!";
private String receiverType = CometChatConstants.RECEIVER_TYPE_GROUP;
TextMessage textMessage = new TextMessage(receiverID, messageText,receiverType);
CometChat.sendMessage(textMessage, new CometChat.CallbackListener<TextMessage>() {
@Override
public void onSuccess(TextMessage textMessage) {
Log.d(TAG, "Message sent successfully: " + textMessage.toString());
}
@Override
public void onError(CometChatException e) {
Log.d(TAG, "Message sending failed with exception: " + e.getMessage());
}
});
val receiverID:String="GUID"
val messageText:String="Hello world!"
val receiverType:String=CometChatConstants.RECEIVER_TYPE_GROUP
val textMessage = TextMessage(receiverID, messageText,receiverType)
CometChat.sendMessage(textMessage, object : CometChat.CallbackListener<TextMessage>() {
override fun onSuccess(p0: TextMessage?) {
Log.d(TAG, "Message sent successfully: " + p0?.toString())
}
override fun onError(p0: CometChatException?) {
Log.d(TAG, "Message sending failed with exception: " + p0?.message)
}
})
The TextMessage
class constructor takes the following parameters:
Parameter | Description | |
---|---|---|
receiverID | UID of the user or GUID of the group receiving the message | Required |
messageText | The text message | Required |
receiverType | The type of the receiver- CometChatConstants.RECEIVER_TYPE_USER (user) or CometChatConstants.RECEIVER_TYPE_GROUP (group) | Required |
When a text message is sent successfully, the response will include a TextMessage
object which includes all information related to the sent message.
Add Metadata
To send custom data along with a text message, you can use the setMetadata
method and pass a JSONObject
to it.
- Java
- Kotlin
JSONObject metadata = new JSONObject();
metadata.put("lattitude", "50.6192171633316");
metadata.put("longitude", "-72.68182268750002");
textMessage.setMetadata(metadata);
val metadataObject:JSONObject=JSONObject("{\"latitude\":\"50.6192171633316\",\"longitude\":\"-72.68182268750002\"}")
textMessage.metadata=metadataObject
Media Message
In other words, as a sender, how do I send a media message like photos, videos & files?
To send a media message to any user or group, you need to use the sendMediaMessage()
method and pass a MediaMessage
object to it.
- Java(User)
- Kotlin(User)
- Java(Group)
- Kotlin(Group)
private String receiverID = "UID";
private String messageType = CometChatConstants.MESSAGE_TYPE_IMAGE;
private String receiverType = CometChatConstants.RECEIVER_TYPE_USER;
private String filePath = "/storage/emulated/0/Download/46.jpg";
MediaMessage mediaMessage = new MediaMessage(receiverID,new File(filePath),messageType,receiverType);
CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener<MediaMessage>() {
@Override
public void onSuccess(MediaMessage mediaMessage) {
Log.d(TAG, "Media message sent successfully: " + mediaMessage.toString());
}
@Override
public void onError(CometChatException e) {
Log.d(TAG, "Media message sending failed with exception: " + e.getMessage());
}
});
val recreceiverID:String = "UID"
val messageType:String = CometChatConstants.MESSAGE_TYPE_IMAGE
val receiverType:String = CometChatConstants.RECEIVER_TYPE_USER
val filePath:String = "/storage/emulated/0/Download/46.jpg"
val mediaMessage = MediaMessage(recreceiverID, File(filePath), messageType, receiverType)
CometChat.sendMediaMessage(mediaMessage, object : CometChat.CallbackListener<MediaMessage>() {
override fun onSuccess(p0: MediaMessage?) {
Log.d(TAG, "Media message sent successfully: " + p0?.toString())
}
override fun onError(p0: CometChatException?) {
Log.d(TAG, "Message sending failed with exception: " + p0?.message)
}
})
private String receiverID = "GUID";
private String messageType = CometChatConstants.MESSAGE_TYPE_IMAGE;
private String receiverType = CometChatConstants.RECEIVER_TYPE_GROUP;
private String filePath = "/storage/emulated/0/Download/46.jpg";
MediaMessage mediaMessage = new MediaMessage(receiverID,new File(filePath),messageType,receiverType);
CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener<MediaMessage>() {
@Override
public void onSuccess(MediaMessage mediaMessage) {
Log.d(TAG, "Media message sent successfully: " + mediaMessage.toString());
}
@Override
public void onError(CometChatException e) {
Log.d(TAG, "Media message sending failed with exception: " + e.getMessage());
}
});
val recreceiverID:String = "GUID"
val messageType:String = CometChatConstants.MESSAGE_TYPE_IMAGE
val receiverType:String = CometChatConstants.RECEIVER_TYPE_GROUP
val filePath:String = "/storage/emulated/0/Download/46.jpg"
val mediaMessage = MediaMessage(recreceiverID, File(filePath), messageType, receiverType)
CometChat.sendMediaMessage(mediaMessage, object : CometChat.CallbackListener<MediaMessage>() {
override fun onSuccess(p0: MediaMessage?) {
Log.d(TAG, "Media message sent successfully: " + p0?.toString())
}
override fun onError(p0: CometChatException?) {
Log.d(TAG, "Message sending failed with exception: " + p0?.message)
}
})
The MediaMessage
class constructor takes the following parameters:
Parameter | Description |
---|---|
receiverId | The UID or GUID of the recipient |
file | The file object to be sent |
messageType | The type of the message that needs to be sent which in this case can be: 1. CometChatConstants.MESSAGE_TYPE_IMAGE (image) 2. CometChatConstants.MESSAGE_TYPE_VIDEO (video)3. CometChatConstants.MESSAGE_TYPE_AUDIO (audio)4. CometChatConstants.MESSAGE_TYPE_FILE (file) |
receiverType | The type of the receiver to whom the message is to be sent i.e CometChatConstants.RECEIVER_TYPE_USER (user) or CometChatConstants.RECEIVER_TYPE_GROUP (group) |
When a media message is sent successfully, the response will include a MediaMessage
object which includes all information related to the sent message.
If you wish to send a caption or some text along with the Media Message, you can use the caption field
provided by the MediaMessage class. To set the caption you can use the setCaption()
method and at the receiver end, you can obtain the caption using the getCaption()
method.
As with text messages, the metadata field can be used with media messages as well. Any additional information can be passed along with the media message as a JSONObject
.
Add Metadata
To send custom data along with a media message, you can use the setMetadata
method and pass a JSONObject
to it.
- Java
- Kotlin
JSONObject metadata = new JSONObject();
metadata.put("lattitude", "50.6192171633316");
metadata.put("longitude", "-72.68182268750002");
textMessage.setMetadata(metadata);
val metadataObject:JSONObject=JSONObject("{\"caption\":\"My visit to Disneyland!\"}")
textMessage.metadata=metadataObject
Custom Message
In other words, as a sender, how do I send a custom message like location co-ordinates?
CometChat allows you to send custom messages which are neither text nor media messages.
In order to send a custom message, you need to use the sendCustomMessage()
method.
The sendCustomMessage()
methods takes an object of the CustomMessage
which can be obtained using the below constructor.
- Java
CustomMessage customMessage = new CustomMessage(receiverId, receiverType,customType, customData)
The above constructor, helps you create a custom message with the message type set to whatever is passed to the constructor and the category set to custom
.
The parameters involved are:
receiverId
- Unique id of the user or group to which the message is to be sent.receiverType
- Type of the receiver i.e user or groupcustomType
- custom message type that you need to setcustomData
- The data to be passed as the message in the form of a JSONObject.
You can also use the subType field of the CustomMessage
class to set a specific type for the custom message. This can be achieved using the setSubtype()
method.
Once the object of CustomMessage
class is ready you can send the custom message using the sendCustomMessage()
method.
- Java
- Kotlin
private String UID = "UID";
private String customType = "LOCATION";
JSONObject customData = new JSONObject();
customData.put("lattitue","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());
}
});
val UID="UID"
val customType="Location"
val customData=JSONObject()
customData.put("lattitue","19.0760")
customData.put("longitude","72.8777")
val customMessage = CustomMessage(UID, CometChatConstants.RECEIVER_TYPE_USER,customType, customData)
CometChat.sendCustomMessage(customMessage, object :CometChat.CallbackListener<CustomMessage>() {
override fun onSuccess(customMessage: CustomMessage) {
Log.d(TAG, customMessage.toString())
}
override fun onError(e: CometChatException) {
Log.d(TAG, e.message)
}
})
The above sample explains how custom messages can be used to share the location with a user. Similarly, you can send custom messages to groups.
On success, you will receive an object of CustomMessage
class.