Skip to main content
Quick Reference for AI Agents & Developers
// Add members to a group
val members = listOf(
    GroupMember("uid1", CometChatConstants.SCOPE_MODERATOR),
    GroupMember("uid2", CometChatConstants.SCOPE_PARTICIPANT)
)

CometChat.addMembersToGroup("GUID", members, null, 
    object : CallbackListener<HashMap<String, String>>() {
        override fun onSuccess(result: HashMap<String, String>) { }
        override fun onError(e: CometChatException) { }
    })
Note: Only admins and moderators can add members. The result HashMap contains UID as key and “success” or error message as value.
Available via: SDK | REST API | UI Kits

Add Members to Group

You can add members to the group using the addMembersToGroup() method. This method takes the below parameters:
  1. GUID - GUID of the group users are to be added to.
  2. List<GroupMember> members - This is a list of GroupMember objects. In order to add members, you need to create an object of the GroupMember class. The UID and the scope of the GroupMember are mandatory.
  3. List<String> bannedMembers - This is the list of UIDs that need to be banned from the group. This can be set to null if there are no members to be banned.
  4. Callback
List<GroupMember> members = new ArrayList<>();
members.add(new GroupMember("uid1",CometChatConstants.SCOPE_MODERATOR));
members.add(new GroupMember("uid2", CometChatConstants.SCOPE_ADMIN));

CometChat.addMembersToGroup("GUID", members, null, new CometChat.CallbackListener<HashMap<String, String>>(){
  @Override
  public void onSuccess(HashMap<String, String> successMap) {
    Log.d("CometChatActivity", success);
  }

  @Override
  public void onError(CometChatException e) {
    Toast.makeText(CometChatActivity.this, e.getMessage(),Toast.LENGTH_LONG).show();
  }
});
In the onSuccess() method of the CallbackListener, you will receive a HashMap which will contain the UID of the users and the value will either be success or an error message describing why the operation to add the user to the group or ban the user failed.

Real-Time Group Member Added Events

How do I know when someone is added to the group while my app is running?
When a group member is added by another member, this event is triggered. When a user joins a group on their own, the joined event is triggered.
To receive real-time events whenever a new member is added to a group, implement the onMemberAddedToGroup() method of the GroupListener class.
  • onMemberAddedToGroup() - This method is triggered when any user is added to the group, informing the logged-in user of the other members added to the group.
CometChat.addGroupListener("UNIQUE_ID", new CometChat.GroupListener() {
  @Override
  public void onMemberAddedToGroup(Action action, User addedby, User userAdded, Group addedTo) {
    Log.d("onMemberAddedToGroup", action.getMessage());
  }
});

Member Added to Group event in Message History

How do I know when someone is added to the group while my app is not running? When you retrieve the list of previous messages, if a member has been added to any group that the logged-in user is a member of, the list of messages will contain an Action message. An Action message is a sub-class of the BaseMessage class. For the group member added event, in the Action object received, the following fields can help you get the relevant information-
  1. action - added
  2. actionOn - User object containing the details of the user who was added to the group
  3. actionBy - User object containing the details of the user who added the member to the group
  4. actionFor - Group object containing the details of the group to which the member was added
Always remove group listeners when they’re no longer needed (e.g., in onDestroy() or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling.

Best Practices

Choose member scopes carefully: ADMIN for full control, MODERATOR for moderation tasks, PARTICIPANT for regular members. Limit admin count to maintain control.
Add multiple members in a single call instead of multiple individual calls to improve performance and reduce API overhead.
Check the result HashMap for each UID - some additions may succeed while others fail. Handle each case appropriately in your UI.
Verify user UIDs exist before attempting to add them to avoid unnecessary API errors.

Troubleshooting

Symptom: addMembersToGroup() fails with “Insufficient permissions” error.Cause: User is not an admin or moderator of the group.Solution: Only admins and moderators can add members. Check the user’s scope before showing add member options.
Symptom: Result HashMap shows errors for some UIDs.Cause: UIDs may not exist, users may already be members, or users may be banned.Solution: Check the error message for each UID in the result HashMap. Verify UIDs exist and users aren’t already members or banned.
Symptom: Adding members to private group fails.Cause: Private groups have restricted membership.Solution: Ensure you have admin or moderator permissions. Private groups require explicit member additions by authorized users.

Next Steps