Group Channel
Group Channel allows users to chat privately in small groups.
A group channel is private conversation. Access to the channel requires invitation from already existing group participants. It is an ideal fit for a small group to discuss a topic privately. Also, it is suitable for direct exchange of chat messages between two users.
Creating a Group Channel
You may create a group channel in your Android app with the help of create()
function of our GroupChannel
class.
GroupChannel.create(CHANNEL_NAME, PARTICIPANTS_LIST, IS_DISTINCT, new GroupChannel.CreateListener() {
@Override
public void onResult(GroupChannel groupChannel, ChatCampException e) {
if(e == null) {
// New group channel has been created.
}
}
});
CHANNEL_NAME
is a string and represents name of your group channel.PARTICIPANTS_LIST
is an array of typeString[]
containing the user IDs.IS_DISTINCT
is a boolean and determines if a new group channel should be created for the same participants. You can set this boolean totrue
in order to reuse existing channel for same set of participants.
Creating One to One Private Chat
One to One Chat is a special case of Group Channel. If a group channel has 2 participants, then it is a one to one private chat. It is also recommended to use IS_DISTINCT
option as true
for one to one chat. It will ensure that there is always a single channel between 2 users and there are no duplicates.
GroupChannel.create(CHANNEL_NAME, [CURRENT_USER_ID, SECOND_PARTICIPANT_ID], true, new GroupChannel.CreateListener() {
@Override
public void onResult(GroupChannel groupChannel, ChatCampException e) {
if(e == null) {
// New group channel has been created.
}
}
});
CHANNEL_NAME
is a string and represents the name of your group channel.CURRENT_USER_ID
is the user id of the current user who is starting one to one chat.SECOND_PARTICIPANT_ID
is the user id with which the current user is starting chatting with.
Getting the list of all my group channels
To retrieve the list of all group channels of the current user, you would need to use GroupChannelListQuery
class. The get()
function of GroupChannelListQuery
retrieves the group channel list from the server and calls the listener once the result is available.
GroupChannelListQuery groupChannelListQuery = GroupChannel.createGroupChannelListQuery();
groupChannelListQuery.get(new GroupChannelListQuery.ResultHandler() {
@Override
public void onResult(List<GroupChannel> groupChannelList) {
// Group Channel List received.
}
});
Getting a Group Channel with ID
You may retrieve a particular group with the help of its ID by calling GroupChannel.get()
. This function takes Group Channel ID as argument along with callback listener.
GroupChannel.get(CHANNEL_ID, new GroupChannel.GetListener() {
@Override
public void onResult(final GroupChannel groupChannel, ChatCampException e) {
if(e == null) {
// Group Channel retrieved successfully.
}
}
});
Inviting Participants to Group Channel
Already existing participants of the group channel can invite more users to join the channel.
GroupChannel.inviteParticipants(PARTICIPANT_IDs, new GroupChannel.InviteParticipantsListener() {
@Override
public void onResult(ChatCampException e) {
if(e == null) {
// Participants added successfully.
}
}
});
PARTICIPANT_IDs
is an array of user_ids (string).- Please note that
inviteParticipants()
is a function of an instance ofGroupChannel
class.
Accept Invitation to Group Channel
Participants on getting an invitation to join a group channel need to accept the invitation to participate in the channel.
GroupChannel.acceptInvitation(new GroupChannel.AcceptInvitationListener() {
@Override
public void onResult(GroupChannel groupChannel, ChatCampException e) {
if(e == null) {
// Invitation accepted successfully.
}
}
});
Decline Invitation of Group Channel
Participants on getting an invitation to join a group channel can also decline the invitation.
GroupChannel.declineInvitation(new GroupChannel.DeclineInvitationListener() {
@Override
public void onResult(ChatCampException e) {
if(e == null) {
// Invitation declined successfully.
}
}
});
Leaving Group Channel
A participant of a group channel can leave the channel if she no longer wants to receive messages or notification from the channel.
GroupChannel.leave(new GroupChannel.LeaveListener() {
@Override
public void onResult(ChatCampException e) {
if(e == null) {
// Channel left successfully.
}
}
});
Sending Messages in Channel
A participant of a group channel can send two types of messages in it:
TEXT
: a normal text messageATTACHMENT
: a binary attachment like image, document etc.
Sending Text Message
The sendMessage()
function allows you to send a text message. You may use onSent()
callback to check if the message has been successfully sent.
GroupChannel.sendMessage(MESSAGE, new BaseChannel.SendMessageListener() {
@Override
public void onSent(Message message, ChatCampException e) {
// Message sent successfully
}
});
You may also send additional properties like METADATA
and CUSTOM_FILTER
while sending a message as shown below:
GroupChannel.sendMessage(MESSAGE, METADATA, CUSTOM_FILTER, new BaseChannel.SendMessageListener() {
@Override
public void onSent(Message message, ChatCampException e) {
// Message sent successfully
}
});
MESSAGE
- Text message which needs to be sent.METADATA
- Map where keys and values should bestring
. It may be used to store additional details about a message.CUSTOM_FILTER
-List<String>
which can be used to categorize messages.
Sending Attachment
The sendAttachment()
function allows you to send a file. The file will be first uploaded to the ChatCamp backend and will be then shared in the channel.
GroupChannel.sendAttachment(FILE, FILE_NAME, CONTENT_TYPE, new GroupChannel.UploadAttachmentListener() {
@Override
public void onUploadProgress(int progress) {
// Upload in progress
}
@Override
public void onUploadSuccess() {
// Upload in success
}
@Override
public void onUploadFailed(Throwable error) {
// Error while uploading file
ChatCampException exception = new ChatCampException(error.getMessage(), "FILE UPLOAD FAILED");
}
});
FILE
- Instance ofFile
which has to be sentFILE_NAME
- Name of the file (String
)CONTENT_TYPE
- File mime type (String
)
Receiving Messages
In ChatCamp, ChannelListener
is used to handle information from the server. This information can be a new chat message, new group channel created by some other user etc.
To receive message in a group channel, onGroupChannelMessageReceived
callback of ChannelListener
is used.
ChatCamp.addChannelListener(CHANNEL_LISTENER_ID, new ChatCamp.ChannelListener() {
@Override
public void onGroupChannelMessageReceived(GroupChannel groupChannel, Message message) {
// New message is received
}
});
Retrieving Previous Messages
Previous messages of a group channel can be retrieved. It can only be retrieved by participants of the group channel.
PreviousMessageListQuery previousMessageListQuery = channel.createPreviousMessageListQuery();
previousMessageListQuery.setReference(REFERENCE); //Optional
previousMessageListQuery.setSortBy(SORT_BY); //Optional
previousMessageListQuery.load(LIMIT, REVERSE, new PreviousMessageListQuery.ResultListener() {
@Override
public void onResult(List<Message> messageList, ChatCampException e) {
// List of messages received
}
});
LIMIT
- The number of messages to be fetched. The maximum limit is100
.REVERSE
- If the messages should be fetched in reverse chronological history. In most of the cases, its value will beTRUE
.REFERENCE
is the referencemessage_id
. Messages older than thismessage_id
will be loaded. In case to load most recent messages, passREFERENCE
asnull
.SORT_BY
means to sort the list by most recently sent or most recently updated messages. Possible values arePreviousMessageListQueryFilter.SORT_BY_INSERTED_AT
orPreviousMessageListQueryFilter.SORT_BY_UPDATED_AT
.
Sending Typing Indicators
Typing indicators can be shown in chat using functions startTyping()
and stopTyping()
. These are functions of instance of GroupChannel
class.
GroupChannel.get(CHANNEL_ID, new GroupChannel.GetListener() {
@Override
public void onResult(final GroupChannel groupChannel, ChatCampException e) {
if(e == null) {
// Group Channel retrieved successfully.
groupChannel.startTyping();
groupChannel.stopTyping();
}
}
});
Receiving Typing Indicators
Typing indicators sent by other users in the group channel can be handled using ChannelListener
functionality of ChatCamp SDK. For Typing indicator, onGroupChannelTypingStatusChanged
may be used. It is similar to receiving messages as illustrated in the above section.
ChatCamp.addChannelListener(CHANNEL_LISTENER_ID, new ChatCamp.ChannelListener() {
@Override
public void onGroupChannelTypingStatusChanged(GroupChannel groupChannel) {
// groupChannel.getTypingParticipants();
}
});
Read Receipt
Current user can mark all messages of a group channel as read by calling function markAsRead()
of GroupChannel
class.
GroupChannel.get(CHANNEL_ID, new GroupChannel.GetListener() {
@Override
public void onResult(final GroupChannel groupChannel, ChatCampException e) {
if(e == null) {
// Group Channel retrieved successfully.
groupChannel.markAsRead();
}
}
});
Updated less than a minute ago