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.
- A Interactive Messages , for sending end-user interactive messages of type form, card and custom Interactive
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.
Add Metadata
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.
- Swift
- Objective C
let metadata = ["latitude":"50.6192171633316","longitude":"-72.68182268750002"];
textMessage.metaData = metadata;
NSDictionary * metadata = @{@"latitude":@"50.6192171633316",@"longitude":@"-72.68182268750002"};
[textMessage setMetaData:metadata];
Add Tags
To add a tag to a message you can use Array
of strings that you need to set it to property tags
.
- Swift
- Objective C
let tags = ["pinned"]
textMessage.tags = tags;
NSArray * tags = @[@"latitude"];
[textMessage setTags:tags];
Once the text message object is ready, you need to use the sendMessage()
method to send the text message to the recipient.
- User(Swift)
- User(Objective C)
- Group(Swift)
- Group(Objective C)
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);
}
NSString *receiverID = @"cometchat-uid-1";
NSString *text = @"Hello";
TextMessage *textMessage = [[TextMessage alloc]initWithReceiverUid:receiverID text:text receiverType:ReceiverTypeUser];
[CometChat sendTextMessageWithMessage:textMessage onSuccess:^(TextMessage * message) {
NSLog(@"TextMessage sent successfully. %@",[message stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"TextMessage sending failed with error: %@",[error errorDescription]);
}];
let receiverID = "cometchat-guid-102"
let text = "Hello"
let textMessage = TextMessage(receiverUid: receiverID, text: text, receiverType: .group)
CometChat.sendTextMessage(message: textMessage, onSuccess: { (message) in
print("TextMessage sent successfully. " + message.stringValue())
}) { (error) in
print("TextMessage sending failed with error: " + error!.errorDescription);
}
NSString *receiverID = @"cometchat-guid-101";
NSString *text = @"Hello";
TextMessage *textMessage = [[TextMessage alloc]initWithReceiverUid:receiverID text:text receiverType:ReceiverTypeGroup];
[CometChat sendTextMessageWithMessage:textMessage onSuccess:^(TextMessage * message) {
NSLog(@"TextMessage sent successfully. %@",[message stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"TextMessage sending failed with error: %@",[error errorDescription]);
}];
The TextMessage
class constructor takes the following parameters:
Parameters | Information |
---|---|
receiverID | The UID or GUID of the recipient |
text | The text to be sent |
receiverType | The 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.
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.
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.
- Swift
- Objective C
let metadata = ["latitude":"50.6192171633316","longitude":"-72.68182268750002"];
mediaMessage.metaData = metadata;
NSDictionary * metadata = @{@"latitude":@"50.6192171633316",@"longitude":@"-72.68182268750002"};
[mediaMessage setMetaData:metadata];
Add Caption(Text along with Media Message)
To send a caption with a media message, you can use setCaption
method and pass text to it.
- Swift
mediaMessage.caption = "Message Caption"
Add Tags
To add a tag to a message you can use Array
of strings that you need to set it to property tags
.
- Swift
- Objective C
let tags = ["pinned"]
textMessage.tags = tags;
NSArray * tags = @[@"latitude"];
[textMessage setTags:tags];
There are 2 ways you can send Media Messages using the CometChat SDK:
- By providing the File : You can directly share the file object while creating an object of the MediaMessage class. When the media message is sent using the sendMediaMessage() method, this file is then uploaded to CometChat servers and the URL of the file is sent in the success response of the sendMediaMessage() function.
- Swift
- Objective C
let receiverid = "cometchat-uid-2"
let mediaUrl = "file:___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);
}
NSString *receiverID = @"cometchat-uid-1";
NSString *filePath = @"Library_Developer_CoreSimulator_Devices_(numbers and letters)_data_Containers_Data_Application_(numbers and letters)_Documents_image.jpg";
MediaMessage *mediaMessage = [MediaMessage alloc]initWithReceiverUid:receiverID fileurl:filePath messageType:messageTypeImage receiverType:ReceiverTypeUser];
[CometChat sendMediaMessageWithMessage:mediaMessage onSuccess:^(MediaMessage * message) {
NSLog(@"MediaMessage sent successfully. %@",[message stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"MediaMessage sending failed with error: %@",[error errorDescription]);
}];
- Swift(Group)
- Objective C(Group)
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: .group);
CometChat.sendMediaMessage(message: mediaMessage, onSuccess: { (message) in
print("MediaMessage sent successfully. " + message.stringValue())
}) { (error) in
print("MediaMessage sending failed with error: " + error.errorDescription);
}
NSString *receiverID = @"cometchat-uid-1";
NSString *filePath = @"Library_Developer_CoreSimulator_Devices_(numbers and letters)_data_Containers_Data_Application_(numbers and letters)_Documents_image.jpg";
MediaMessage *mediaMessage = [MediaMessage alloc]initWithReceiverUid:receiverID fileurl:filePath messageType:messageTypeImage receiverType:ReceiverTypeGroup];
[CometChat sendMediaMessageWithMessage:mediaMessage onSuccess:^(MediaMessage * message) {
NSLog(@"MediaMessage sent successfully. %@",[message stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"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:
Parameter | Description | |
---|---|---|
receiverId | The UID or GUID of the recipient | Required |
fileurl | The file path object to be sent | Required |
messageType | The type of the message that needs to be sent which in this case can be: - image - video - audio - file | Required |
receiverType | The type of the receiver to whom the message is to be sent - user - group | Required |
- By providing the URL of the File: The second way to send media messages using the CometChat SDK is to provide the SDK with the URL of any file that is hosted on your servers or any cloud storage. To achieve this you will have to make use of the Attachment class that is available in the MediaMessage class. For more information, you can refer to the below code snippet:
- Swift
let receiverid = "cometchat-uid-2"
let mediaUrl = "https:__pngimg.com_uploads_mario_mario_PNG125.png"
let mediaMessage = MediaMessage(receiverUid: receiverid, fileurl:"", messageType: .image, receiverType: .user);
mediaMessage.attachment = Attachment(fileName: "FileName", fileExtension: "png", fileMimeType: "image_png", fileUrl: mediaUrl);
CometChat.sendMediaMessage(message: mediaMessage, onSuccess: { (message) in
print("MediaMessage sent successfully. " + message.stringValue())
}) { (error) in
print("MediaMessage sending failed with error: " + error.errorDescription);
}
On success, you will receive an object of the MediaMessage
class containing all the information related to the sent media 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
.
Multiple Attachments in a Media Message
Starting version 3.0.906 & above the SDK supports sending of multiple attachments in a single media message. As in case for single attachment in a media message, there are two ways you can send Media Messages using the CometChat SDK:
- By providing an array of files: You can now share a List of files while creating an object of the MediaMessage class. When the media message is sent using the
sendMediaMessage()
method, the files are uploaded to the CometChat servers & the URL of the files are sent in the success response of thesendMediaMessage()
method.
- Swift
String receiverId = "cometchat-uid-1"
let data = try? Data(contentsOf: URL(string: " file:___private_var_mobile_Containers_Data_Application_B71B0A14_tmp_1111.jpeg")
let data1 = try? Data(contentsOf: URL(string: " file:___private_var_mobile_Containers_Data_Application_B71B0A14_tmp_2222.jpeg")
let data2 = try? Data(contentsOf: URL(string: " file:___private_var_mobile_Containers_Data_Application_B71B0A14_tmp_3333.jpeg")
var files = [File]()
files.append(File(name: "FileName 1", data: data)
files.append(File(name: "FileName 2", data: data1)
files.append(File(name: "FileName 3", data: data2)
mediaMessage = MediaMessage(receiverUid: self.currentUser?.uid ?? "", files: files, messageType: .file, receiverType: .user)
CometChat.sendMediaMessage(message: mediaMessage, onSuccess: { (message) in
print("Message sent successfully")
}, onError: { (error) in
print("Message sending failed with exception : ", error?.errorDescription)
}
- Swift(Group)
mediaMessage = MediaMessage(receiverUid: self.currentUser?.uid ?? "", files: withFileArray, messageType: .file, receiverType: .user)
The MediaMessage
class constructor takes the following parameters:
Parameter | Description |
---|---|
receiverId | The UID or GUID of the recipient. |
files | An array of File object. File object is made of name and data of the file |
messageType | The type of the message that needs to be sent which in this case can be: 1. CometChatConstants.MESSAGE_TYPE_IMAGE 2. CometChatConstants.MESSAGE_TYPE_VIDEO 3. CometChatConstants.MESSAGE_TYPE_AUDIO 4. CometChatConstants.MESSAGE_TYPE_FILE |
receiverType | The type of the receiver to whom the message is to be sent. 1. CometChatConstants.RECEIVER_TYPE_USER 2. CometChatConstants.RECEIVER_TYPE_GROUP |
- By providing the URL of the multiple files: The second way to send multiple attachments using the CometChat SDK is to provide the SDK with the URL of multiple files that is hosted on your servers or any cloud storage. To achieve this you will have to make use of the
Attachment
class. For more information, you can refer to the below code snippet:
- Swift
let mediaMessage = MediaMessage(receiverUid: self.currentUser?.uid ?? "", files: [], messageType: .file, receiverType: .user)
var attachments = [Attachment]()
for i in 0..<5 {
let attachement = Attachment(fileName: "test", fileExtension: "png", fileMimeType: "img_png", fileUrl: "https:__pngimg.com_uploads_mario_mario_PNG125.png")
attachments.append(attachement)
}
mediaMessage.attachments = attachments
CometChat.sendMediaMessage(message: mediaMessage!, onSuccess: { (message) in
print("Message Sent Successfully"
}) { (error) in
print("Message Sending Failed with exception : ",error.errorDesciption)
}
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 respectively in exactly the same way as it is done while sending a single file or attachment in a Media Message.
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:
- Swift
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:
- 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 group
- type - custom message type that you need to set
- 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.
Add Tags
To add a tag to a message you can use Array
of strings that you need to set it to property tags
.
- Swift
- Objective C
let tags = ["pinned"]
textMessage.tags = tags;
NSArray * tags = @[@"latitude"];
[textMessage setTags:tags];
Once the object of CustomMessage
class is ready you can send the custom message using the sendCustomMessage()
method.
- User(Swift)
- User(Objective C)
- Group(Swift)
- Group(Objective C)
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);
}
NSString *receiverID = @"cometchat-uid-1";
NSMutableDictionary<NSString *, id> *customData = [NSMutableDictionary new];
[customData setObject:@"customData" forKey:@"customKey"];
CustomMessage *customMessage = [CustomMessage alloc]initWithReceiverUid:receiverID receiverType:ReceiverTypeUser customData: customData type:@"Custom Type"];
[CometChat sendCustomMessageWithMessage:customMessage onSuccess:^(CustomMessage * message) {
NSLog(@"CustomMessage sent successfully. %@",[message stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"CustomMessage sending failed with error: %@",[error errorDescription]);
}];
let receiverid = "cometchat-uid-2"
let customData : [String : Any] = ["customKey" : "customData"]
let customMessage = CustomMessage(receiverUid: receiverid, receiverType: .group, 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);
}
NSString *receiverID = @"cometchat-uid-1";
NSMutableDictionary<NSString *, id> *customData = [NSMutableDictionary new];
[customData setObject:@"customData" forKey:@"customKey"];
CustomMessage *customMessage = [CustomMessage alloc]initWithReceiverUid:receiverID receiverType:ReceiverTypeGroup customData: customData type:@"Custom Type"];
[CometChat sendCustomMessageWithMessage:customMessage onSuccess:^(CustomMessage * message) {
NSLog(@"CustomMessage sent successfully. %@",[message stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"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.
Update Conversation
How can I decide whether the custom message should update last message of a conversation?
By default, a custom message will update the last message of a conversation. If you wish to not update last message of conversation when a custom message is sent, please use updateConversation property of the Custom Message.
- User(Swift)
- User(Objective C)
- Group(Swift)
- Group(Objective C)
let receiverid = "cometchat-uid-2"
let customData : [String : Any] = ["customKey" : "customData"]
let customMessage = CustomMessage(receiverUid: receiverid, receiverType: .user, customData : customData, type: "Custom Type");
customMessage.updateConversation = false //for not updating conversation
CometChat.sendCustomMessage(message: customMessage, onSuccess: { (message) in
print("CustomMessage sent successfully. " + message.stringValue())
}) { (error) in
print("CustomMessage sending failed with error: " + error!.errorDescription);
}
NSString *receiverID = @"cometchat-uid-1";
NSMutableDictionary<NSString *, id> *customData = [NSMutableDictionary new];
[customData setObject:@"customData" forKey:@"customKey"];
CustomMessage *customMessage = [CustomMessage alloc]initWithReceiverUid:receiverID receiverType:ReceiverTypeUser customData: customData type:@"Custom Type"];
customMessage.updateConversation = NO; //for not updating conversation
[CometChat sendCustomMessageWithMessage:customMessage onSuccess:^(CustomMessage * message) {
NSLog(@"CustomMessage sent successfully. %@",[message stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"CustomMessage sending failed with error: %@",[error errorDescription]);
}];
let receiverid = "cometchat-uid-2"
let customData : [String : Any] = ["customKey" : "customData"]
let customMessage = CustomMessage(receiverUid: receiverid, receiverType: .group, customData : customData, type: "Custom Type");
customMessage.updateConversation = false //for not updating conversation
CometChat.sendCustomMessage(message: customMessage, onSuccess: { (message) in
print("CustomMessage sent successfully. " + message.stringValue())
}) { (error) in
print("CustomMessage sending failed with error: " + error!.errorDescription);
}
NSString *receiverID = @"cometchat-uid-1";
NSMutableDictionary<NSString *, id> *customData = [NSMutableDictionary new];
[customData setObject:@"customData" forKey:@"customKey"];
CustomMessage *customMessage = [CustomMessage alloc]initWithReceiverUid:receiverID receiverType:ReceiverTypeGroup customData: customData type:@"Custom Type"];
[customMessage]
customMessage.updateConversation = NO; //for not updating conversation
[CometChat sendCustomMessageWithMessage:customMessage onSuccess:^(CustomMessage * message) {
NSLog(@"CustomMessage sent successfully. %@",[message stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"CustomMessage sending failed with error: %@",[error errorDescription]);
}];
Custom Notification Body
How can i customise the notification body of custom message?
To add a custom notification body for Push, Email & SMS
notification of a custom message you can use the conversationText
property of Custom Message class.
- User(Swift)
- User(Objective C)
- Group(Swift)
- Group(Objective C)
let receiverid = "cometchat-uid-2"
let customData : [String : Any] = ["customKey" : "customData"]
let customMessage = CustomMessage(receiverUid: receiverid, receiverType: .user, customData : customData, type: "Custom Type");
customMessage.conversationText = "Custom notification body";
CometChat.sendCustomMessage(message: customMessage, onSuccess: { (message) in
print("CustomMessage sent successfully. " + message.stringValue())
}) { (error) in
print("CustomMessage sending failed with error: " + error!.errorDescription);
}
NSString *receiverID = @"cometchat-uid-1";
NSMutableDictionary<NSString *, id> *customData = [NSMutableDictionary new];
[customData setObject:@"customData" forKey:@"customKey"];
CustomMessage *customMessage = [CustomMessage alloc]initWithReceiverUid:receiverID receiverType:ReceiverTypeUser customData: customData type:@"Custom Type"];
customMessage.conversationText = "Custom notification body";
[CometChat sendCustomMessageWithMessage:customMessage onSuccess:^(CustomMessage * message) {
NSLog(@"CustomMessage sent successfully. %@",[message stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"CustomMessage sending failed with error: %@",[error errorDescription]);
}];
let receiverid = "cometchat-uid-2"
let customData : [String : Any] = ["customKey" : "customData"]
let customMessage = CustomMessage(receiverUid: receiverid, receiverType: .group, customData : customData, type: "Custom Type");
customMessage.conversationText = "Custom notification body";
CometChat.sendCustomMessage(message: customMessage, onSuccess: { (message) in
print("CustomMessage sent successfully. " + message.stringValue())
}) { (error) in
print("CustomMessage sending failed with error: " + error!.errorDescription);
}
NSString *receiverID = @"cometchat-uid-1";
NSMutableDictionary<NSString *, id> *customData = [NSMutableDictionary new];
[customData setObject:@"customData" forKey:@"customKey"];
CustomMessage *customMessage = [CustomMessage alloc]initWithReceiverUid:receiverID receiverType:ReceiverTypeGroup customData: customData type:@"Custom Type"];
[customMessage]
customMessage.conversationText = "Custom notification body";
[CometChat sendCustomMessageWithMessage:customMessage onSuccess:^(CustomMessage * message) {
NSLog(@"CustomMessage sent successfully. %@",[message stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"CustomMessage sending failed with error: %@",[error errorDescription]);
}];
It is also possible to send interactive messages from CometChat , to know more click here