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
List<GroupMember> members = [
  GroupMember.fromUid(uid: "UID", scope: CometChatMemberScope.participant, name: "User 1"),
];

CometChat.addMembersToGroup(
  guid: "GUID",
  groupMembers: members,
  onSuccess: (Map<String?, String?> result) {
    debugPrint("Members added: $result");
  },
  onError: (CometChatException e) {
    debugPrint("Error: ${e.message}");
  },
);

// Listen for member added events
class MyGroupListener with GroupListener {
  @override
  void onMemberAddedToGroup(Action action, User addedby, User userAdded, Group addedTo) {
    debugPrint("Member added: ${userAdded.name}");
  }
}
CometChat.addGroupListener("listenerId", MyGroupListener());
Add users to a group programmatically. Only admins and moderators can add members. The added users receive a notification and are immediately part of the group.

Add Members to Group

Available via: SDK | REST API | UI Kits
Use addMembersToGroup() to add members to a Group.
ParameterTypeDescription
guidStringThe GUID of the group to add members to
groupMembersList<GroupMember>List of GroupMember objects (UID and scope required)
onSuccessFunction(Map<String?, String?>)Callback with a map of UIDs to result status
onErrorFunction(CometChatException)Callback triggered on failure
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.
List<GroupMember> groupMembers = [];
GroupMember firstMember = GroupMember.fromUid(
scope: CometChatMemberScope.participant,
uid: "cometchat-uid-3",name: "name");
GroupMember secondMember = GroupMember.fromUid(scope: CometChatMemberScope.admin,
uid: "cometchat-uid-4",name: "name");

groupMembers = [firstMember, secondMember];

CometChat.addMembersToGroup( guid: conversationWith,
        groupMembers: groupMembers,
        onSuccess: (Map<String?,String?> result) {
          debugPrint("Group Member added Successfully : $result");
        },
        onError: (CometChatException e) {
          debugPrint("Group Member addition failed with exception: ${e.message}");
        });
On Success — A Map<String?, String?> where each key is the UID of the user and the value is either "success" or an error message:
ParameterTypeDescriptionSample Value
cometchat-uid-3stringResult for the first member added"success"
cometchat-uid-4stringResult for the second member added"success"
ParameterTypeDescriptionSample Value
codestringError code identifier"ERR_GUID_NOT_FOUND"
messagestringHuman-readable error message"The specified group does not exist."
detailsstringAdditional technical details"Please provide a valid group GUID."
In the onSuccess() callback, you will receive a Map 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 failed.

Real-Time Group Member Added Events

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.
Implement onMemberAddedToGroup() in GroupListener to receive real-time notifications when members are added.
class Class_Name  with GroupListener {

//CometChat.addGroupListener("group_Listener_id", this);
@override
void onMemberAddedToGroup(Action action, User addedby, User userAdded, Group addedTo) {
  print("onMemberAddedToGroup");
}
}
Callback ParameterTypeDescription
actionActionThe action object containing details of the event
addedbyUserThe user who added the member
userAddedUserThe user who was added to the group
addedToGroupThe group the member was added to
Always remove group listeners when they’re no longer needed (e.g., in dispose()). Failing to remove listeners can cause memory leaks and duplicate event handling.
CometChat.removeGroupListener("group_Listener_id");

Missed Member Added Events

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 BaseMessage class. For the group member added event, in the Action object received, the following fields can help you get the relevant information:
FieldValue/TypeDescription
action"added"The action type
actionOnUserThe user who was added
actionByUserThe user who added the member
actionForGroupThe group the member was added to

Next Steps

Kick & Ban Members

Remove or ban members from a group

Change Member Scope

Promote or demote group members

Retrieve Group Members

Fetch the list of members in a group

Join a Group

Allow users to join public or password-protected groups