Quick Reference for AI Agents & Developers
Create a Group
As a logged-in user, how do I create a public, private, or password-protected group? You can create a group using thecreateGroup() method. This method takes a Group object as input.
To create an object of the Group class, you can use either of the following constructors:
new Group(String GUID, String name, String groupType, String password)new Group(String GUID, String name, String groupType, String password, String icon, String description)
groupType needs to be one of the following values:
CometChatConstants.GROUP_TYPE_PUBLIC(public)CometChatConstants.GROUP_TYPE_PASSWORD(password)CometChatConstants.GROUP_TYPE_PRIVATE(private)
- Java
- Kotlin
createGroup() method takes the following parameters:
| Parameter | Description |
|---|---|
group | An instance of Group class |
Group class which contains all the information about the particular group.
Add members while creating a group
You can create a group and add members at the same time using thecreateGroupWithMembers() method. This method takes the Group object, an array of GroupMember objects to be added, and an array of UIDs to be banned.
To create an object of the Group class, you can use either of the following constructors:
new Group(String GUID, String name, String groupType, String password)new Group(String GUID, String name, String groupType, String password, String icon, String description)
groupType needs to be one of the following values:
CometChat.GROUP_TYPE.PUBLICCometChat.GROUP_TYPE.PASSWORDCometChat.GROUP_TYPE.PRIVATE
GroupMember class, you can use the following constructor:
new GroupMember(String UID, String scope)
- Java
- Kotlin
onSuccess() block of this method provides you with 2 sets of information:
Group: The group object containing information about the group that was created.HashMap<String, String>: A HashMap that contains the UID of the user that was supposed to be added as the key andsuccessor an error message as the value.
Group Class
| Field | Editable | Information |
|---|---|---|
| guid | Needs to be specified at group creation. Cannot be edited later | A unique identifier for a group |
| name | Yes | Name of the group |
| type | No | Type of the group: Can be 1. Public 2. Password 3. Private |
| password | No | Password for the group in case the group is of type password. |
| icon | Yes | An URL to group icon |
| description | Yes | Description about the group |
| owner | Yes | UID of the owner of the group. |
| metadata | Yes | Additional data for the group as JSON |
| createdAt | No | The unix timestamp of the time the group was created |
| updatedAt | No | The unix timestamp of the time the group was last updated |
| hasJoined | No | A boolean to determine if the logged in user is a member of the group. |
| joinedAt | No | The unix timestamp of the time the logged in user joined the group. |
| scope | Yes | Scope of the logged in user. Can be: 1. Admin 2. Moderator 3. Participant |
| membersCount | No | The number of members in the groups |
| tags | Yes | A list of tags to identify specific groups. |
Best Practices
Use Descriptive Group Names and GUIDs
Use Descriptive Group Names and GUIDs
Choose meaningful GUIDs and names that help identify groups. GUIDs should be alphanumeric with underscores/hyphens only - avoid spaces and special characters.
Set Appropriate Group Type
Set Appropriate Group Type
Use PUBLIC for open communities, PRIVATE for invite-only groups, and PASSWORD for semi-private groups. Choose based on your privacy requirements.
Add Metadata for Custom Properties
Add Metadata for Custom Properties
Use the metadata field to store custom group properties like categories, tags, or business-specific data without creating custom group types.
Troubleshooting
Group Creation Failed - Invalid GUID
Group Creation Failed - Invalid GUID
Symptom:
createGroup() fails with “Invalid GUID” error.Cause: GUID contains spaces, punctuation, or special characters other than underscore and hyphen.Solution: Use only alphanumeric characters, underscores, and hyphens in GUIDs. Example: "my_group_123" or "group-abc-456".Group Already Exists
Group Already Exists
Symptom:
createGroup() fails with “Group already exists” error.Cause: A group with the same GUID already exists in your app.Solution: Use unique GUIDs for each group. Consider using UUIDs or timestamp-based identifiers to ensure uniqueness.