val GUID:String="GUID"val groupType:String=CometChatConstants.GROUP_TYPE_PUBLICval password:String=""CometChat.joinGroup(GUID,groupType,password,object:CometChat.CallbackListener<Group>(){ override fun onSuccess(p0: Group?) { Log.d(TAG, p0.toString()); } override fun onError(p0: CometChatException?) { Log.d(TAG, "Group joining failed with exception: " + p0?.message) }})
The joinGroup() method takes the following parameters:
Parameter
Description
GUID
The GUID of the group you would like to join
groupType
Type of the group. CometChat provides 3 types of groups: 1. CometChatConstants.GROUP_TYPE_PUBLIC (public) 2. CometChatConstants.GROUP_TYPE_PASSWORD (password) 3. CometChatConstants.GROUP_TYPE_PRIVATE (private)
password
Password is mandatory for password-protected groups.
Once you have joined a group successfully, you can send and receive messages in that group.CometChat keeps track of the groups you have joined, so you do not need to join the group every time you want to communicate in it.You can identify if a group is joined using the hasJoined parameter in the Group object.
When a user joins a group, members receive a real-time event in onGroupMemberJoined() of the GroupListener class. The callback provides an Action object, the joined User, and the Group.
Java
Kotlin
CometChat.addGroupListener(UNIQUE_LISTENER_ID, new CometChat.GroupListener() { @Override public void onGroupMemberJoined(Action action, User joinedUser, Group joinedGroup) { Log.d(TAG, "User joined"); }});
When fetching message history, if a member joined a group the logged-in user is part of, the list will contain an Action message with these fields:
action - joined
actionBy - User object containing the details of the user who joined the group
actionFor - Group object containing the details of the group the user has joined
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.