> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-rn-guide-message-privately.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Send Messages

> Send CometChat text, media, and custom messages to users and groups with the iOS SDK using success and error callbacks.

<Accordion title="AI Integration Quick Reference">
  | Field          | Value                                                                                                                                                                  |
  | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | Key Classes    | [`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), [`CustomMessage`](/sdk/reference/messages#custommessage) |
  | Key Methods    | `sendTextMessage()`, `sendMediaMessage()`, `sendCustomMessage()`                                                                                                       |
  | Receiver Types | `.user`, `.group`                                                                                                                                                      |
  | Message Types  | `.text`, `.image`, `.video`, `.audio`, `.file`, custom                                                                                                                 |
  | Prerequisites  | SDK initialized, user logged in                                                                                                                                        |
</Accordion>

CometChat supports three types of messages:

| Type                      | Method                | Use Case                          |
| ------------------------- | --------------------- | --------------------------------- |
| [Text](#text-message)     | `sendTextMessage()`   | Plain text messages               |
| [Media](#media-message)   | `sendMediaMessage()`  | Images, videos, audio, files      |
| [Custom](#custom-message) | `sendCustomMessage()` | Location, polls, or any JSON data |

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

Send a text message using `CometChat.sendTextMessage()` with a [`TextMessage`](/sdk/reference/messages#textmessage) object.

<Tabs>
  <Tab title="Swift (User)">
    ```swift theme={null}
    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);
    }
    ```
  </Tab>

  <Tab title="Objective-C (User)">
    ```objc theme={null}
    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]);
    }];
    ```
  </Tab>

  <Tab title="Swift (Group)">
    ```swift theme={null}
    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);
    }
    ```
  </Tab>

  <Tab title="Objective-C (Group)">
    ```objc theme={null}
    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]);
    }];
    ```
  </Tab>
</Tabs>

The [`TextMessage`](/sdk/reference/messages#textmessage) class constructor takes the following parameters:

| Parameter      | Description                                                | Required |
| -------------- | ---------------------------------------------------------- | -------- |
| `receiverID`   | UID of the user or GUID of the group receiving the message | Yes      |
| `text`         | The text message content                                   | Yes      |
| `receiverType` | `.user` or `.group`                                        | Yes      |

On success, `sendTextMessage()` returns a [`TextMessage`](/sdk/reference/messages#textmessage) object containing all information about the sent message.

### Add Metadata

Send custom data along with a text message using the `metaData` property:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let metadata = ["latitude":"50.6192171633316","longitude":"-72.68182268750002"];
    textMessage.metaData = metadata;
    ```
  </Tab>

  <Tab title="Objective-C">
    ```objc theme={null}
    NSDictionary * metadata = @{@"latitude":@"50.6192171633316",@"longitude":@"-72.68182268750002"};
    [textMessage setMetaData:metadata];
    ```
  </Tab>
</Tabs>

### Add Tags

```swift theme={null}
let tags = ["pinned"]
textMessage.tags = tags
```

### Quote a Message

```swift theme={null}
textMessage.quotedMessageId = 140
textMessage.quotedMessage = // Pass the BaseMessage object you want to quote
```

## Media Message

Send images, videos, audio, or files using `CometChat.sendMediaMessage()`.

There are two ways to send media messages:

1. **Upload a file** — Pass a file path and CometChat uploads it automatically
2. **Send a URL** — Provide a URL to media hosted on your server or cloud storage

### Upload a File

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let receiverid = "cometchat-uid-2"
    let mediaUrl = "file:///Library/Developer/CoreSimulator/.../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);
    }
    ```
  </Tab>

  <Tab title="Objective-C">
    ```objc theme={null}
    NSString *receiverID = @"cometchat-uid-1";
    NSString *filePath = @"Library/Developer/CoreSimulator/.../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]);
    }];
    ```
  </Tab>
</Tabs>

### Send a URL

Use the [`Attachment`](/sdk/reference/auxiliary#attachment) class to send media hosted on your server or cloud storage:

```swift theme={null}
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);
}
```

The [`MediaMessage`](/sdk/reference/messages#mediamessage) class constructor takes the following parameters:

| Parameter      | Description                              | Required |
| -------------- | ---------------------------------------- | -------- |
| `receiverID`   | UID of the user or GUID of the group     | Yes      |
| `fileurl`      | File path or empty string if using URL   | Yes      |
| `messageType`  | `.image`, `.video`, `.audio`, or `.file` | Yes      |
| `receiverType` | `.user` or `.group`                      | Yes      |

On success, `sendMediaMessage()` returns a [`MediaMessage`](/sdk/reference/messages#mediamessage) object.

### Add Caption

```swift theme={null}
mediaMessage.caption = "Message Caption"
```

### Add Metadata and Tags

Same as text messages:

```swift theme={null}
mediaMessage.metaData = ["location": "Paris"]
mediaMessage.tags = ["vacation"]
```

## Multiple Attachments in a Media Message

Starting version 3.0.906 & above, the SDK supports sending multiple attachments in a single media message.

```swift theme={null}
let receiverId = "cometchat-uid-1"

let data = try? Data(contentsOf: URL(string: "file:///path/to/image1.jpeg")!)
let data1 = try? Data(contentsOf: URL(string: "file:///path/to/image2.jpeg")!)

var files = [File]()
files.append(File(name: "FileName 1", data: data))
files.append(File(name: "FileName 2", data: data1))

let mediaMessage = MediaMessage(receiverUid: receiverId, files: files, messageType: .file, receiverType: .user)

CometChat.sendMediaMessage(message: mediaMessage, onSuccess: { (message) in
  print("Message sent successfully")
}, onError: { (error) in
  print("Message sending failed: ", error?.errorDescription)
})
```

## Custom Message

Send structured data that doesn't fit text or media categories — like location coordinates, polls, or game moves.

<Tabs>
  <Tab title="Swift (User)">
    ```swift theme={null}
    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);
    }
    ```
  </Tab>

  <Tab title="Swift (Group)">
    ```swift theme={null}
    let receiverid = "cometchat-guid-1"
    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);
    }
    ```
  </Tab>
</Tabs>

The [`CustomMessage`](/sdk/reference/messages#custommessage) class constructor takes the following parameters:

| Parameter      | Description                                            | Required |
| -------------- | ------------------------------------------------------ | -------- |
| `receiverID`   | UID of the user or GUID of the group                   | Yes      |
| `receiverType` | `.user` or `.group`                                    | Yes      |
| `customData`   | Dictionary with your custom data                       | Yes      |
| `type`         | Your custom type string (e.g., `"location"`, `"poll"`) | Yes      |

On success, `sendCustomMessage()` returns a [`CustomMessage`](/sdk/reference/messages#custommessage) object.

### Add Tags

```swift theme={null}
customMessage.tags = ["starredMessage"]
```

### Control Conversation Update

By default, custom messages update the conversation's last message. To prevent this:

```swift theme={null}
customMessage.updateConversation = false
```

### Custom Notification Text

Set custom text for push, email, and SMS notifications:

```swift theme={null}
customMessage.conversationText = "Shared a location"
```

<Note>
  It is also possible to send interactive messages from CometChat. To learn more, see [Interactive Messages](/sdk/ios/interactive-messages).
</Note>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Receive Messages" icon="inbox" href="/sdk/ios/receive-message">
    Listen for incoming messages in real-time
  </Card>

  <Card title="Edit Message" icon="pen" href="/sdk/ios/edit-message">
    Edit previously sent messages
  </Card>

  <Card title="Delete Message" icon="trash" href="/sdk/ios/delete-message">
    Delete sent messages
  </Card>
</CardGroup>
