Create a Group

Learn how to create a group.

Create a Group

In other words, as a logged-in user, how do I create a public, private or password-protected group?

You can create a group using createGroup() method. This method takes a Group object as input.

To create an object of Group class, you can use either of the below two constructors:

  1. new Group(String GUID, String name, String groupType, String password)
  2. new Group(String GUID, String name, String groupType, String password, String icon, String description)

The groupType needs to be either of the below 3 values:

1.CometChatConstants.GROUP_TYPE_PUBLIC (public)
2.CometChatConstants.GROUP_TYPE_PASSWORD (password)
3.CometChatConstants.GROUP_TYPE_PRIVATE (private)

For more information on the group class, please check here

private String GUID = "GUID";
private String groupName = "Hello Group!";
private String groupType = CometChatConstants.GROUP_TYPE_PUBLIC;
private String password = "";

Group group = new Group(GUID, groupName, groupType, password);

CometChat.createGroup(group, new CometChat.CallbackListener<Group>(){
  @Override
  public void onSuccess(Group group) {
    Log.d(TAG, "Group created successfully: " + group.toString());
  }
  @Override
  public void onError(CometChatException e) {
    Log.d(TAG, "Group creation failed with exception: " + e.getMessage());
  }
});
val GUID:String="GUID"
val groupName:String="Hello Group"
val groupType:String=CometChatConstants.GROUP_TYPE_PUBLIC
val password:String=""

val group=Group(GUID,groupName,groupType,password)

CometChat.createGroup(group,object :CometChat.CallbackListener<Group>(){
  override fun onSuccess(p0: Group?) {
    Log.d(TAG, "Group created successfully: " + p0?.toString())
  }
  override fun onError(p0: CometChatException?) {
    Log.d(TAG, "Group creation failed with exception: " + p0?.message)
  }
})

The createGroup() method takes the following parameters:

Parameter

Description

group

An instance of Group class

After the successful creation of the group, you will receive an instance of Group class which contains all the information about the particular group.

🚧

GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed.

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.


Did this page help you?