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

# Send A Message

> Send CometChat text, media, custom, and interactive messages to users and groups from Flutter apps.

Using CometChat, you can send three types of messages:

1. A [Text Message](/sdk/flutter/send-message#text-message), the most common and standard message type.
2. A [Media Message](/sdk/flutter/send-message#media-message), for sending photos, videos and files.
3. A [Custom Message](/sdk/flutter/send-message#custom-message), for sending completely custom data using Map structures.
4. A [Interactive Messages](/sdk/flutter/interactive-messages) , for sending end-user interactive messages of type form, card and custom Interactive

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.

### Add Metadata

To send custom data along with a text message, you can use the `setMetadata` method and pass a `Map` to it.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    Map<String,String> metadata = {};
    metadata["lattitude"] =  "50.6192171633316" ;
    metadata["longitude"]  = "-72.68182268750002" ;
    textMessage.metadata = metadata;
    ```
  </Tab>
</Tabs>

### Add Tags

To add a tag to a message you can assign value in `.tags` variable of the TextMessage Class. `tags` accepts a list of tags.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    List<String> tags = [];
    tags.add("pinned");
    textMessage.tags = tags;
    ```
  </Tab>
</Tabs>

Once the text message object is ready, you need to use the `sendMessage()` method to send the text message to the recipient.

<Tabs>
  <Tab title="Dart (User)">
    ```dart theme={null}
    String receiverID = "cometchat-uid-1";
    String messageText = "messageText";
    String receiverType = CometChatConversationType.user;
    String type = CometChatMessageType.text;

    TextMessage textMessage = TextMessage(text: messageText,
                                        receiverUid: receiverID,
              													receiverType: receiverType,
               													type: type);
    CometChat.sendMessage(textMessage, 
      onSuccess: (TextMessage message) {
    	debugPrint("Message sent successfully:  $message");
      }, onError: (CometChatException e) {
    	debugPrint("Message sending failed with exception:  ${e.message}");
      }
    );
    ```
  </Tab>

  <Tab title="Dart (group)">
    ```dart theme={null}
    String receiverID = "cometchat-guid-1";
    String messageText = "messageText";
    String receiverType = CometChatConversationType.group;
    String type = CometChatMessageType.text;

    TextMessage textMessage = TextMessage(text: messageText,
                                        receiverUid: receiverID,
              													receiverType: receiverType,
               													type: type);
    CometChat.sendMessage(textMessage, onSuccess: (TextMessage message) {
    debugPrint("Message sent successfully:  $message");
    }, onError: (CometChatException e) {
    debugPrint("Message sending failed with exception:  ${e.message}");
    });
    ```
  </Tab>
</Tabs>

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- `CometChatReceiverType.user` (user) or `CometChatReceiverType.group` (group)         | Required |
| type           | The type of the message that needs to be sent which in this case can be: `CometChatMessageType.text`\_\_(text) |          |

When a text message is sent successfully, the response will include a `TextMessage` object which includes all information related to the sent message.

### Set Quoted Message

To set a quoted message for a message, use the `setQuotedMessageId` and `setQuotedMessage` method of the TextMessage class. This method accepts the ID of the message to be quoted.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    textMessage.quotedMessageId = 0
    textMessage.quotedMessage =  // Pass base message object here that you want to quote.
    ```
  </Tab>
</Tabs>

Once the text message object is ready, you need to use the `sendMessage()` method to send the text message to the recipient.

<Tabs>
  <Tab title="Dart (User)">
    ```dart theme={null}
    String receiverID = "UID";
      String messageText = "Hello CometChat!";
      String receiverType = CometChatReceiverType.user;

      TextMessage textMessage =
          TextMessage(receiverID, messageText, receiverType);

      CometChat.sendMessage(
        textMessage,
        onSuccess: (TextMessage message) {
          print("Message sent successfully: ${message.toString()}");
        },
        onError: (CometChatException e) {
          print("Message sending failed with exception: ${e.message}");
        },
      );
    ```
  </Tab>

  <Tab title="Dart (Group)">
    ```dart theme={null}
    String receiverID = "GUID";
      String messageText = "Hello CometChat!";
      String receiverType = CometChatReceiverType.group;

      TextMessage textMessage =
          TextMessage(receiverID, messageText, receiverType);

      CometChat.sendMessage(
        textMessage,
        onSuccess: (TextMessage message) {
          print("Message sent successfully: ${message.toString()}");
        },
        onError: (CometChatException e) {
          print("Message sending failed with exception: ${e.message}");
        },
      );
    ```
  </Tab>
</Tabs>

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.

## 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 `setMetadata` method and pass a `Map` to it.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    Map<String,dynamic> metadata = {};
    metadata["lattitude"] =  "50.6192171633316" ;
    metadata["longitude"]  = "-72.68182268750002" ;
    mediaMessage.metadata = metadata;
    ```
  </Tab>
</Tabs>

### Add Caption(Text along with Media Message)

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    mediaMessage.caption = "Message Caption";
    ```
  </Tab>
</Tabs>

### Add Tags

To add a tag to a message you can use the `setTags()` method of the MediaMessage Class. The `setTags()` method accepts a list of tags.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    List<String> tags = [];
    tags.add("pinned");
    mediaMessage.tags = tags;
    ```
  </Tab>
</Tabs>

There are 2 ways you can send Media Messages using the CometChat SDK:

1. **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.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    String receiverID;
    String messageType = CometChatMessageType.image;
    String receiverType = CometChatConversationType.user ;
    String filePath = "storage/emulated/0/Download/46.jpg";
    MediaMessage mediaMessage = MediaMessage(
    receiverType: receiverType,
    type: messageType,
    receiverUid: receiverID,
    file: filePath);

    await CometChat.sendMediaMessage(mediaMessage, 
      onSuccess: (MediaMessage message) {
      	debugPrint("Media message sent successfully:${mediaMessage.metadata}");
      }, onError: (e) {
      	debugPrint("Media message sending failed with exception: ${e.message}");
      }
    ); 
    ```
  </Tab>
</Tabs>

The `MediaMessage` class constructor takes the following parameters:

| Parameter    | Description                                                                                                                                                                                                                                                      |          |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| receiverId   | The UID or GUID of the recipient                                                                                                                                                                                                                                 | Required |
| file         | The file object to be sent                                                                                                                                                                                                                                       | Required |
| messageType  | The type of the message that needs to be sent which, in this case, can be: <br />1. `CometChatMessageType.image` (image) <br />2. `CometChatMessageType.video` (video) <br />3. `CometChatMessageType.audio` (audio) <br />4. `CometChatMessageType.file` (file) | Required |
| receiverType | The type of the receiver to whom the message is to be sent, i.e., `CometChatReceiverType.user` (user) or `CometChatReceiverType.group` (group)                                                                                                                   | Required |

2. **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:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    MediaMessage mediaMessage = MediaMessage(
                              receiverType: receiverType,
                              type: messageType,
                              receiverUid: receiverID,
                              file: null);

    String fileUrl  = "https://pngimg.com/uploads/mario/mario_PNG125.png";
    String fileName   = "test";
    String fileExtension = "png";
    String fileMimeType = "image/png";

    Attachment attach =  Attachment(fileUrl,fileName,fileExtension,fileMimeType,null);
    mediaMessage.attachment= attach;

    await CometChat.sendMediaMessage(mediaMessage,
      onSuccess: (MediaMessage message) {
      	debugPrint( "Media message sent successfully: ${mediaMessage}");                            
      }, onError: (CometChatException e) {
      	debugPrint("Media message sending failed with exception: ${e.message}");
      }
    );   
    ```
  </Tab>
</Tabs>

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 get and set the caption you can use the `.caption` variable . 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 `Map`.

### Send Multiple Media Files

You can send multiple media files (images, videos, audio, or documents) in a single message. This enables richer and more efficient conversations.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    String receiverID = "cometchat-uid-1";
    String receiverType = CometChatConversationType.user;
    String messageType = CometChatMessageType.image;

    // Create a list of file paths
    List<String> filePaths = [
      "storage/emulated/0/Download/image1.jpg",
      "storage/emulated/0/Download/image2.jpg",
      "storage/emulated/0/Download/image3.jpg",
    ];

    MediaMessage mediaMessage = MediaMessage(
      receiverType: receiverType,
      type: messageType,
      receiverUid: receiverID,
      files: filePaths,  // Use 'files' for multiple files
    );

    await CometChat.sendMediaMessage(mediaMessage, 
      onSuccess: (MediaMessage message) {
        debugPrint("Multiple media files sent successfully");
        // Access the list of attachments
        List<Attachment>? attachments = message.attachments;
        for (var attachment in attachments ?? []) {
          debugPrint("Attachment URL: ${attachment.fileUrl}");
        }
      }, 
      onError: (CometChatException e) {
        debugPrint("Media message sending failed: ${e.message}");
      }
    );
    ```
  </Tab>
</Tabs>

### File Size and Count Validation

The SDK automatically validates file size and count when sending media messages. You can configure these limits through Settings in the CometChat Dashboard.

| Validation | Description                                                     |
| ---------- | --------------------------------------------------------------- |
| File Size  | Maximum size per file (configurable in Dashboard)               |
| File Count | Maximum number of files per message (configurable in Dashboard) |

If validation fails, the `onError` callback will be triggered with details about the validation error.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    await CometChat.sendMediaMessage(mediaMessage, 
      onSuccess: (MediaMessage message) {
        debugPrint("Media message sent successfully");
      }, 
      onError: (CometChatException e) {
        // Handle validation errors
        if (e.code == "ERR_FILE_SIZE_EXCEEDED") {
          debugPrint("File size exceeds the allowed limit");
        } else if (e.code == "ERR_FILE_COUNT_EXCEEDED") {
          debugPrint("Too many files in a single message");
        } else {
          debugPrint("Error: ${e.message}");
        }
      }
    );
    ```
  </Tab>
</Tabs>

## 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.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    CustomMessage customMessage = CustomMessage( receiverUid: UID,
        type: type,
        customData: customData,
        receiverType: receiverType,
        subType: subType,
      );
    ```
  </Tab>
</Tabs>

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. `receiverUid` - 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. `customType` - custom message type that you need to set
4. `customData` - The data to be passed as the message in the form of a `Map`.

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 `subtype` field.

### Add Tags

To add a tag to a message you can assign value in `.tags` variable of the CustomMessage Class. `tags` accepts a list of tags.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    List<String> tags = [];
    tags.add("pinned");
    textMessage.tags = tags;
    ```
  </Tab>
</Tabs>

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

<Tabs>
  <Tab title="User">
    ```dart theme={null}
    String UID = "UID";
    String subType = "LOCATION";
    String receiverType = CometChatConversationType.user;
    String type = CometChatMessageType.custom;

    Map<String, String> customData = {};
    customData["latitude"] = "19.0760";
    customData["longitude"] = "72.8777";

    CustomMessage customMessage = CustomMessage(
      receiverUid: UID,
      type: type,
      customData: customData,
      receiverType: receiverType,
      subType: subType,
    );

    CometChat.sendCustomMessage(customMessage, onSuccess: (CustomMessage message) {
      debugPrint("Custom Message Sent Successfully : $message");
    }, onError: (CometChatException e) {
      debugPrint("Custom message sending failed with exception: ${e.message}");
    });
    ```
  </Tab>

  <Tab title="Group">
    ```dart theme={null}
    String GUID = "GUID";
    String subType = "LOCATION";
    String receiverType = CometChatConversationType.group;
    String type = CometChatMessageType.custom;

    Map<String, String> customData = {};
    customData["latitude"] = "19.0760";
    customData["longitude"] = "72.8777";

    CustomMessage customMessage = CustomMessage(
      receiverUid: GUID,
      type: type,
      customData: customData,
      receiverType: receiverType,
      subType: subType,
    );

    CometChat.sendCustomMessage(customMessage, onSuccess: (CustomMessage message) {
      debugPrint("Custom Message Sent Successfully : $message");
    }, onError: (CometChatException e) {
      debugPrint("Custom message sending failed with exception: ${e.message}");
    });
    ```
  </Tab>
</Tabs>

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.

### Update Conversation

*How can I decide whether the custom message should update the last message of a conversation?*

By default, a custom message will update the last message of a conversation. If you wish to not update the last message of the conversation when a custom message is sent, please use `updateConversation` (boolean value) method of the Custom Message.

<Tabs>
  <Tab title="User">
    ```dart theme={null}
    String UID = "UID";
    String subType = "LOCATION";
    String receiverType = CometChatConversationType.user;
    String type = CometChatMessageType.custom;

    Map<String, String> customData = {};
    customData["latitude"] = "19.0760";
    customData["longitude"] = "72.8777";

    CustomMessage customMessage = CustomMessage(
      receiverUid: UID,
      type: type,
      customData: customData,
      receiverType: receiverType,
      subType: subType,
    );
    customMessage.updateConversation = false;

    CometChat.sendCustomMessage(customMessage, onSuccess: (CustomMessage message) {
      debugPrint("Custom Message Sent Successfully : $message");
    }, onError: (CometChatException e) {
      debugPrint("Custom message sending failed with exception: ${e.message}");
    });
    ```
  </Tab>

  <Tab title="Group">
    ```dart theme={null}
    String GUID = "GUID";
    String subType = "LOCATION";
    String receiverType = CometChatConversationType.group;
    String type = CometChatMessageType.custom;

    Map<String, String> customData = {};
    customData["latitude"] = "19.0760";
    customData["longitude"] = "72.8777";

    CustomMessage customMessage = CustomMessage(
      receiverUid: GUID,
      type: type,
      customData: customData,
      receiverType: receiverType,
      subType: subType,
    );
    customMessage.updateConversation = false;

    CometChat.sendCustomMessage(customMessage, onSuccess: (CustomMessage message) {
      debugPrint("Custom Message Sent Successfully : $message");
    }, onError: (CometChatException e) {
      debugPrint("Custom message sending failed with exception: ${e.message}");
    });
    ```
  </Tab>
</Tabs>

### 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` method of Custom Message class.

<Tabs>
  <Tab title="User">
    ```dart theme={null}
    String UID = "UID";
    String subType = "LOCATION";
    String receiverType = CometChatConversationType.user;
    String type = CometChatMessageType.custom;

    Map<String, String> customData = {};
    customData["latitude"] = "19.0760";
    customData["longitude"] = "72.8777";

    CustomMessage customMessage = CustomMessage(
      receiverUid: UID,
      type: type,
      customData: customData,
      receiverType: receiverType,
      subType: subType,
    );
    customMessage.conversationText = "Custom Notification Body";

    CometChat.sendCustomMessage(customMessage, onSuccess: (CustomMessage message) {
      debugPrint("Custom Message Sent Successfully : $message");
    }, onError: (CometChatException e) {
      debugPrint("Custom message sending failed with exception: ${e.message}");
    });
    ```
  </Tab>

  <Tab title="Group">
    ```dart theme={null}
    String GUID = "GUID";
    String subType = "LOCATION";
    String receiverType = CometChatConversationType.group;
    String type = CometChatMessageType.custom;

    Map<String, String> customData = {};
    customData["latitude"] = "19.0760";
    customData["longitude"] = "72.8777";

    CustomMessage customMessage = CustomMessage(
      receiverUid: GUID,
      type: type,
      customData: customData,
      receiverType: receiverType,
      subType: subType,
    );
    customMessage.conversationText = "Custom Notification Body";

    CometChat.sendCustomMessage(customMessage, onSuccess: (CustomMessage message) {
      debugPrint("Custom Message Sent Successfully : $message");
    }, onError: (CometChatException e) {
      debugPrint("Custom message sending failed with exception: ${e.message}");
    });
    ```
  </Tab>
</Tabs>

<Note>
  It is also possible to send interactive messages from CometChat , to know more [click here](/sdk/flutter/interactive-messages)
</Note>
