Skip to main content

Documentation Index

Fetch the complete documentation index at: https://cometchat-22654f5b-docs-android-v6-beta2.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

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

Add Members to Group

Use addMembersToGroup() with the group GUID, a list of GroupMember objects, and an optional list of UIDs to ban.
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

When a member is added to a group, existing members receive a real-time event in onMemberAddedToGroup() of the GroupListener class. The callback provides an Action object, the adding User, the added User, and the Group.
When a member is added by another user, onMemberAddedToGroup() fires. When a user joins on their own, onGroupMemberJoined() fires instead.
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

When fetching message history, if a member was added to a group the logged-in user is part of, the list will contain an Action message with these fields:
  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.

GroupMember Payload Structure

The GroupMember object extends User and contains all User fields plus group-specific fields:
ParameterTypeDescription
uidStringUnique identifier of the user
nameStringDisplay name of the user
avatarStringURL to user’s profile picture
linkStringURL to user’s profile page
roleStringUser role for access control
metadataJSONObjectCustom data set by developer
statusStringUser online status. Values: "online", "offline"
statusMessageStringCustom status message
lastActiveAtlongUnix timestamp of last activity
hasBlockedMebooleanWhether this user has blocked the logged-in user
blockedByMebooleanWhether the logged-in user has blocked this user
tagsArray<String>List of tags for user identification
deactivatedAtlongUnix timestamp when user was deactivated (0 if active)
scopeStringMember’s scope in the group. Values: "admin", "moderator", "participant"
joinedAtlongUnix timestamp when member joined the group
Sample GroupMember Object:
{
  "uid": "user_123",
  "name": "John Doe",
  "avatar": "https://example.com/avatar.png",
  "link": "https://example.com/profile",
  "role": "default",
  "metadata": {
    "department": "Engineering",
    "title": "Senior Developer"
  },
  "status": "online",
  "statusMessage": "Available",
  "lastActiveAt": 1699900000,
  "hasBlockedMe": false,
  "blockedByMe": false,
  "tags": ["premium", "verified"],
  "deactivatedAt": 0,
  "scope": "admin",
  "joinedAt": 1699850000
}

Next Steps

Kick Member

Remove members from groups

Change Member Scope

Update member roles and permissions

Retrieve Members

Fetch list of group members

Group Listeners

Handle real-time group events