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

# Flag Message

> Report inappropriate messages to moderators using the CometChat Android SDK flagging system.

<Accordion title="AI Integration Quick Reference">
  ```kotlin theme={null}
  // Get available flag reasons
  CometChat.getFlagReasons(object : CometChat.CallbackListener<MutableList<FlagReason?>>() {
      override fun onSuccess(reasons: MutableList<FlagReason?>?) {
          // Display reasons to user
      }
      override fun onError(e: CometChatException) { }
  })

  // Flag a message
  val flagDetail = FlagDetail().apply {
      reasonId = "spam"
      remark = "Promotional content"
  }
  CometChat.flagMessage(messageId, flagDetail, object : CometChat.CallbackListener<String?>() {
      override fun onSuccess(response: String?) { }
      override fun onError(e: CometChatException?) { }
  })
  ```
</Accordion>

## Overview

Flagging messages allows users to report inappropriate content to moderators or administrators. When a message is flagged, it appears in the [CometChat Dashboard](https://app.cometchat.com) under **Moderation > Flagged Messages** for review.

<Note>
  For a complete understanding of how flagged messages are reviewed and managed, see the [Flagged Messages](/moderation/flagged-messages) documentation.
</Note>

## Prerequisites

Before using the flag message feature:

1. Moderation must be enabled for your app in the [CometChat Dashboard](https://app.cometchat.com)
2. Flag reasons should be configured under **Moderation > Advanced Settings**

## How It Works

```mermaid theme={null}
sequenceDiagram
    participant User
    participant App
    participant SDK
    participant CometChat
    participant Dashboard

    User->>App: Reports message
    App->>SDK: getFlagReasons()
    SDK-->>App: Available reasons
    App->>User: Show reason picker
    User->>App: Select reason + remark
    App->>SDK: flagMessage(id, flagDetail)
    SDK->>CometChat: Flag message
    CometChat-->>SDK: Success response
    SDK-->>App: Message flagged
    CometChat->>Dashboard: Message in Flagged queue
```

## Get Flag Reasons

Before flagging a message, retrieve the list of available flag reasons configured in your Dashboard:

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    CometChat.getFlagReasons(object : CometChat.CallbackListener<MutableList<FlagReason?>>() {
        override fun onSuccess(reasons: MutableList<FlagReason?>?) {
            Log.d(TAG, "Flag reasons fetched: $reasons")
            // Use reasons to populate your report dialog UI
            reasons?.forEach { reason ->
                Log.d(TAG, "Reason ID: ${reason?.id}, Title: ${reason?.reason}")
            }
        }

        override fun onError(e: CometChatException) {
            Log.e(TAG, "Error fetching flag reasons: ${e.message}")
        }
    })
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    CometChat.getFlagReasons(new CometChat.CallbackListener<List<FlagReason>>() {
        @Override
        public void onSuccess(List<FlagReason> reasons) {
            Log.d(TAG, "Flag reasons fetched: " + reasons);
            // Use reasons to populate your report dialog UI
            for (FlagReason reason : reasons) {
                Log.d(TAG, "Reason ID: " + reason.getId() + ", Title: " + reason.getReason());
            }
        }

        @Override
        public void onError(CometChatException e) {
            Log.e(TAG, "Error fetching flag reasons: " + e.getMessage());
        }
    });
    ```
  </Tab>
</Tabs>

### Response

The response is a list of `FlagReason` objects containing:

| Property | Type   | Description                      |
| -------- | ------ | -------------------------------- |
| id       | String | Unique identifier for the reason |
| reason   | String | Display text for the reason      |

## Flag a Message

To flag a message, use the `flagMessage()` method with the message ID and a `FlagDetail` object:

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    val messageId = 123L  // ID of the message to flag

    val flagDetail = FlagDetail().apply {
        reasonId = "spam"  // Required: ID from getFlagReasons()
        remark = "This message contains promotional content"  // Optional
    }

    CometChat.flagMessage(messageId, flagDetail, object : CometChat.CallbackListener<String?>() {
        override fun onSuccess(response: String?) {
            Log.d(TAG, "Message flagged successfully: $response")
        }

        override fun onError(e: CometChatException?) {
            Log.e(TAG, "Message flagging failed: ${e?.message}")
        }
    })
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    long messageId = 123L;  // ID of the message to flag

    FlagDetail flagDetail = new FlagDetail();
    flagDetail.setReasonId("spam");  // Required: ID from getFlagReasons()
    flagDetail.setRemark("This message contains promotional content");  // Optional

    CometChat.flagMessage(messageId, flagDetail, new CometChat.CallbackListener<String>() {
        @Override
        public void onSuccess(String response) {
            Log.d(TAG, "Message flagged successfully: " + response);
        }

        @Override
        public void onError(CometChatException e) {
            Log.e(TAG, "Message flagging failed: " + e.getMessage());
        }
    });
    ```
  </Tab>
</Tabs>

### Parameters

| Parameter           | Type       | Required | Description                                     |
| ------------------- | ---------- | -------- | ----------------------------------------------- |
| messageId           | long       | Yes      | The ID of the message to flag                   |
| flagDetail          | FlagDetail | Yes      | Contains flagging details                       |
| flagDetail.reasonId | String     | Yes      | ID of the flag reason (from `getFlagReasons()`) |
| flagDetail.remark   | String     | No       | Additional context or explanation from the user |

### Response

```json theme={null}
{
  "message": "Message {id} has been flagged successfully."
}
```

## Complete Example

Here's a complete implementation showing how to build a report message flow:

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    class ReportMessageHandler {
        private var flagReasons: List<FlagReason?> = emptyList()

        // Load flag reasons (call this on app init or when needed)
        fun loadFlagReasons(callback: (List<FlagReason?>) -> Unit) {
            CometChat.getFlagReasons(object : CometChat.CallbackListener<MutableList<FlagReason?>>() {
                override fun onSuccess(reasons: MutableList<FlagReason?>?) {
                    flagReasons = reasons ?: emptyList()
                    callback(flagReasons)
                }

                override fun onError(e: CometChatException) {
                    Log.e(TAG, "Failed to load flag reasons: ${e.message}")
                    callback(emptyList())
                }
            })
        }

        // Get reasons for UI display
        fun getReasons(): List<FlagReason?> = flagReasons

        // Flag a message with selected reason
        fun flagMessage(
            messageId: Long,
            reasonId: String,
            remark: String? = null,
            callback: (Boolean, String?) -> Unit
        ) {
            val flagDetail = FlagDetail().apply {
                this.reasonId = reasonId
                remark?.let { this.remark = it }
            }

            CometChat.flagMessage(messageId, flagDetail, object : CometChat.CallbackListener<String?>() {
                override fun onSuccess(response: String?) {
                    callback(true, response)
                }

                override fun onError(e: CometChatException?) {
                    callback(false, e?.message)
                }
            })
        }
    }

    // Usage
    val reportHandler = ReportMessageHandler()

    // Load reasons when app initializes
    reportHandler.loadFlagReasons { reasons ->
        // Display reasons in UI for user to select
    }

    // When user submits the report
    reportHandler.flagMessage(123L, "spam", "User is sending promotional links") { success, message ->
        if (success) {
            showToast("Message reported successfully")
        }
    }
    ```
  </Tab>
</Tabs>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Flagged Messages" icon="flag" href="/moderation/flagged-messages">
    Review and manage flagged messages in the Dashboard
  </Card>

  <Card title="AI Moderation" icon="shield" href="/sdk/android/ai-moderation">
    Automatically detect and filter inappropriate content
  </Card>

  <Card title="Block Users" icon="user-slash" href="/sdk/android/block-users">
    Allow users to block other users from contacting them
  </Card>

  <Card title="Delete Message" icon="trash" href="/sdk/android/delete-message">
    Delete messages from conversations
  </Card>
</CardGroup>
