Quick Reference for AI Agents & Developers
Overview
Flagging messages allows users to report inappropriate content to moderators or administrators. When a message is flagged, it appears in the CometChat Dashboard under Moderation > Flagged Messages for review.For a complete understanding of how flagged messages are reviewed and managed, see the Flagged Messages documentation.
Prerequisites
Before using the flag message feature:- Moderation must be enabled for your app in the CometChat Dashboard
- Flag reasons should be configured under Moderation > Advanced Settings
How It Works
Get Flag Reasons
Before flagging a message, retrieve the list of available flag reasons configured in your Dashboard:- Kotlin
- Java
Response
The response is a list ofFlagReason 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 theflagMessage() method with the message ID and a FlagDetail object:
- Kotlin
- Java
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
Complete Example
Here’s a complete implementation showing how to build a report message flow:- Kotlin
Best Practices
Load Flag Reasons on App Init
Load Flag Reasons on App Init
Fetch flag reasons once during app initialization and cache them locally. This reduces API calls and provides instant access when users need to report messages.
Provide Clear UI for Reporting
Provide Clear UI for Reporting
Display flag reasons in a user-friendly dialog or bottom sheet. Include the optional remark field to allow users to provide additional context about why they’re reporting the message.
Confirm Before Flagging
Confirm Before Flagging
Show a confirmation dialog before submitting the flag to prevent accidental reports. Explain to users that flagged messages will be reviewed by moderators.
Handle Duplicate Flags Gracefully
Handle Duplicate Flags Gracefully
If a user tries to flag the same message multiple times, handle the error gracefully and inform them the message has already been reported.
Troubleshooting
Moderation Not Enabled
Moderation Not Enabled
Symptom:
getFlagReasons() or flagMessage() fails with “Moderation is not enabled” error.Cause: Moderation feature is not enabled for your app in the CometChat Dashboard.Solution: Enable moderation in the CometChat Dashboard under Moderation > Settings. Configure flag reasons under Advanced Settings.Invalid Reason ID
Invalid Reason ID
Symptom:
flagMessage() fails with “Invalid reason ID” error.Cause: The reasonId provided doesn’t match any configured flag reasons.Solution: Always use reason IDs from the getFlagReasons() response. Verify the reason ID exists in your Dashboard configuration.Message Already Flagged
Message Already Flagged
Symptom:
flagMessage() fails with “Message already flagged” error.Cause: The user has already flagged this message previously.Solution: Inform the user that they’ve already reported this message. Consider disabling the flag button for previously flagged messages.