Sending Messages

Learn how to send different types of messages.

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.


Send Text Message to User

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 = "SUPERHERO2"
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 = @"SUPERHERO01";
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]);
}];

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:

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.


Send Text Message to Group

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 = "SUPERGROUP02"
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 = @"SUPERGROUP01";
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]);
}];

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:

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.

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;
NSDictionary * metadata = @{@"latitude":@"50.6192171633316",@"longitude":@"-72.68182268750002"};

[textMessage setMetaData: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 = "SUPERHERO2"

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);
}
let receiverid = "SUPERHERO2"

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 = @"SUPERHERO01";

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]);
    
}];
NSString *receiverID = @"SUPERHERO01";

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

fileurl

The file path object to be sent

messageType

The type of the message that needs to be sent which in this case can be:

  • image
  • video
  • audio
  • file

receiverType

The 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;
NSDictionary * metadata = @{@"latitude":@"50.6192171633316",@"longitude":@"-72.68182268750002"};

[mediaMessage setMetaData: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 = "SUPERHERO2"

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);
}
let receiverid = "SUPERHERO2"

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 = @"SUPERHERO01";

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]);
    
}];
NSString *receiverID = @"SUPERHERO01";

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.


Did this page help you?