Skip to main content
Version: v2

Send a Message

Using CometChat, you can send three types of messages:

  1. A text message, the most common and standard message type.
  2. A media message, for sending photos, videos and files.
  3. A custom message, for sending completely custom data using JSON structures.

You can also send metadata along with a text, media or custom 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.

let receiverID = "cometchat-uid-2"
let text = "Hello"

let textMessage = TextMessage(receiverUid: receiverID, text: text, receiverType: .user)

CometChat.sendTextMessage(message: textMessage, onSuccess: { (message) in

print("TextMessage sent successfully. " + message.stringValue())

}) { (error) in

print("TextMessage sending failed with error: " + error!.errorDescription);
}

To send a message you need to create an object of the TextMessage class. The initialize method of the TextMessage class takes the following mandatory parameters:

The TextMessage class constructor takes the following parameters:

ParametersInformation
receiverIDThe UID or GUID of the recipient
textThe text to be sent
receiverTypeThe type of the receiver to whom the message is to be sent i.e user or group

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 Text Message

To send custom data along with a text message, you can use the metadata field provided in TextMessage. A metadata field is a dictionary of type [String: Any] which will be received as it was sent without any processing from CometChat. It can be used to send any additional data that needs to be sent along with a message.

let metadata = ["latitude":"50.6192171633316","longitude":"-72.68182268750002"];

textMessage.metaData = metadata;

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.

let receiverid = "cometchat-uid-2"

let mediaUrl = "Library/Developer/CoreSimulator/Devices/(numbers and letters)/data/Containers/Data/Application/(numbers and letters)/Documents/image.jpg"

let mediaMessage = MediaMessage(receiverUid: receiverid, fileurl:mediaUrl, messageType: .image, receiverType: .user);

CometChat.sendMediaMessage(message: mediaMessage, onSuccess: { (message) in

print("MediaMessage sent successfully. " + message.stringValue())

}) { (error) in

print("MediaMessage sending failed with error: " + error.errorDescription);
}

To send a media message you need to create an object of the MediaMessage class. The initialize method of the MediaMessage class takes the following mandatory parameters:

ParameterDescription
receiverIdThe UID or GUID of the recipient
fileurlThe file path object to be sent
messageTypeThe type of the message that needs to be sent which in this case can be:
- image
- video
- audio
- file
receiverTypeThe type of the receiver to whom the message is to be sent
- user
- group

On success, you will receive an object of the MediaMessage class containing all the information related to the sent media message.

Add Metadata

To send custom data along with a media message, you can use the metadata field provided in MediaMessage. A metadata field is a dictionary of type [String: Any] which will be received as it was sent without any processing from CometChat. It can be used to send any additional data that needs to be sent along with a message.

let metadata = ["latitude":"50.6192171633316","longitude":"-72.68182268750002"];

mediaMessage.metaData = metadata;

Custom Message

CometChat SDK allows you to send a completely custom message across. You can use this feature to send messages that do not fit in any default categories provided. 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 two constructor:

let customMessage : CustomMessage = CustomMessage(receiverId: receiverId, receiverType: .user, customData : customData, type: "Custom Type")

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:

  1. receiverId - Unique id of the user or group to which the message is to be sent.
  2. receiverType - Type of the receiver i.e user or group
  3. type - custom message type that you need to set
  4. customData - The data to be passed as the message in the form of a Dictionary object.

You can also use the subType field of the CustomMessage class to set a specific type for the CustomeMessage.

Once the object of CustomMessage class is ready you can send the custom message using the sendCustomMessage() method.

let receiverid = "cometchat-uid-2"

let customData : [String : Any] = ["customKey" : "customData"]

let customMessage = CustomMessage(receiverUid: receiverid, receiverType: .user, customData : customData, type: "Custom Type");

CometChat.sendCustomMessage(message: customMessage, onSuccess: { (message) in

print("CustomMessage sent successfully. " + message.stringValue())

}) { (error) in

print("CustomMessage sending failed with error: " + error!.errorDescription);
}

The above sample explains how custom messages can be used to share the location with a user. The same can be achieved for groups.

In success, you will receive an object of the CustomMessage class.