Tutorials

Applozic Android SDK Tutorial 3 – Chat Conversations

Integrate Applozic Android SDK to get started with your own chat application!
Google+ Pinterest LinkedIn Tumblr

This tutorial is Part 3 of a multi-part series covering Applozic Android SDK integration into an Android app.

Our goal is to learn the following steps in this tutorial:

  1. Launch Conversation list screen
  2. Create individual chat thread
  3. Create group chat thread
  4. Create context based chat thread
  5. Delete chat threads

You can download the sample Android chat app from Applozic GitHub.

Conversation screen

To give you an idea of what the conversation screen would look like in the final app, take a look at the screenshot below.

conversation screen
Android Conversation screen

To get started with such a conversation screen, add the following code:

Intent intent = new Intent(this, ConversationActivity.class);            
startActivity(intent);

You can set this to activate as soon as the user opens the conversation screen.

Individual Chat

From the main conversation screen, the user can choose to launch either an individual conversation thread or a group chat thread.

An individual chat screen will look like this:

Individual chat screen
Individual chat screen

For starting individual conversation thread, set “userId” in intent:

Intent intent = new Intent(this, ConversationActivity.class);            
intent.putExtra(ConversationUIService.USER_ID, "receiveruserid123");   
intent.putExtra(ConversationUIService.DISPLAY_NAME, "Receiver display name"); 
//to display the title.
intent.putExtra(ConversationUIService.TAKE_ORDER,true); 
//Skip chat list on back press 
startActivity(intent);

Group Chat

A group conversation contains multiple users that can send and receive messages. Internally, a group conversation is referred to as a channel.

Applozic supports multiple group types that serve different purposes.

ValueNameDescription
1Private GroupPrivate groups are not accessible to contacts other than the ones present in the group. Only group admin can add/remove members in the group. You can create private groups to facilitate close, WhatsApp style interactions. For example, doctor-patient chats.
2Public GroupUsers will be able to search and join Public groups. Any group member can add/remove members in this group. For example, Public groups can be used to create area-wise communities in a social app where users can search their area’s community and join.
5Broadcast GroupA message can be sent to several contacts at once. Messages sent in the broadcast group are received by the members of the group as one-to-one chat thread. Only the sender can see the broadcast group. The broadcast group works on a similar behavior as that of WhatsApp broadcasts.
6Open GroupUsed for free-flowing chats such as live streaming chats, live event chats etc. You need not add any members to this group. All users present on the chat screen will receive messages via MQTT (publish/subscribe pattern). Push notifications are not supported in Open groups.
7GroupOfTwoUsed for the purpose where dealers are selling products and any particular user wants to chat about multiple products with the same dealer. At that time, it is required to show two different chat threads in the conversation history as the context (product) of the chats is different.
9Contacts GroupUsed for creating Friends/Favorites contact List. These lists can be maintained on the application level and any member of the group can access it. For example, an event management company can use the lists to maintain attendees of an event at one place.
10Support GroupBest suited for people who want to use Applozic’s sister product, Kommunicate. These groups are specifically built for the purpose of customer support chat and may contain multiple team members and chatbots talking to a user.
Table showcasing different group types

Launch Group Chat conversation 

You can use the parameter of groupId or channelKey to launch a group chat with the following code:

AlGroupInformationAsyncTask.GroupMemberListener taskListener = new AlGroupInformationAsyncTask.GroupMemberListener() {
  @Override
  public void onSuccess(Channel channel, Context context) {
   Intent chatIntent = new Intent(context, ConversationActivity.class);
   chatIntent.putExtra(ConversationUIService.GROUP_ID, channel.getKey());
   chatIntent.putExtra(ConversationUIService.GROUP_NAME, channel.getName());
   chatIntent.putExtra(ConversationUIService.TAKE_ORDER, true);
   context.startActivity(chatIntent);
  }

  @Override
  public void onFailure(Channel channel, Exception e, Context context) {

  }
 };
 AlGroupInformationAsyncTask groupInfoTask = new AlGroupInformationAsyncTask( < Your Context > , < GroupID > , taskListener);
 AlTask.execute(groupInfoTask);

You can also launch chat using a Client groupId like this:

AlGroupInformationAsyncTask.GroupMemberListener taskListener = new AlGroupInformationAsyncTask.GroupMemberListener() {
 @Override
 public void onSuccess(Channel channel, Context context) {
  Intent chatIntent = new Intent(context, ConversationActivity.class);
  chatIntent.putExtra(ConversationUIService.GROUP_ID, channel.getKey());
  chatIntent.putExtra(ConversationUIService.GROUP_NAME, channel.getName());
  chatIntent.putExtra(ConversationUIService.TAKE_ORDER, true);
  context.startActivity(chatIntent);
 }

 @Override
 public void onFailure(Channel channel, Exception e, Context context) {

 }
};
AlGroupInformationAsyncTask groupInfoTask = new AlGroupInformationAsyncTask( < Your Context > , < ClientGroupId > , taskListener);

Context Based Chat

Context based chat will create and launch a chat group of two based on a shared context. This will be a separate thread conversation for a specific topic with the same user. To enable context based chat, first add the setting in UserLoginTask onSuccess method as below:

ApplozicClient.getInstance(context).setContextBasedChat(true);

Then, start ConversationActivity by adding CONTEXT_BASED_CHAT flag in intent as below:

Intent intent = new Intent(this, ConversationActivity.class);
intent.putExtra(ConversationUIService.CONTEXT_BASED_CHAT,true);
startActivity(intent);

Next, create a context chat conversation:

private Conversation buildConversation() {

       //Title and subtitles are required if you are enabling the view for a particular context.

        TopicDetail topic = new TopicDetail();
        topic.setTitle("Hyundai i20");//Your Topic title
        topic.setSubtitle("May be your car model");//Put Your Topic subtitle
        topic.setLink("Topic Image link if any");

       //You can set two custom key-value pairs which will appear on context view.

        topic.setKey1("Mileage  : ");
        topic.setValue1("18 kmpl");
        topic.setKey2("Price :");
        topic.setValue2("RS. 5.7 lakh");

        //Create Conversation.

        Conversation conversation = new Conversation();

        //SET UserId for which you want to launch chat or conversation

        conversation.setTopicId("Your Topic Id //unique ");
        conversation.setUserId("RECEIVER USERID");
        conversation.setTopicDetail(topic.getJson());
        return conversation;
    }

Finally, create AlAsyncTask that will be the background running task:

ApplozicConversationCreateTask applozicConversationCreateTask = null;

   ApplozicConversationCreateTask.ConversationCreateListener conversationCreateListener =  new ApplozicConversationCreateTask.ConversationCreateListener() {
            @Override
            public void onSuccess(Integer conversationId, Context context) {

                //For launching the  one to one  chat
                Intent intent = new Intent(context, ConversationActivity.class);
                intent.putExtra("takeOrder", true);
                intent.putExtra(ConversationUIService.USER_ID, "userId");//RECEIVER USERID
                intent.putExtra(ConversationUIService.DEFAULT_TEXT, "Hello I am interested in this car, Can we chat?");
                intent.putExtra(ConversationUIService.DISPLAY_NAME,"display name");
                intent.putExtra(ConversationUIService.CONTEXT_BASED_CHAT,true);
                intent.putExtra(ConversationUIService.CONVERSATION_ID,conversationId);
                startActivity(intent);
            }

            @Override
            public void onFailure(Exception e, Context context) {

            }
        };
    Conversation conversation = buildConversation(); //From Step 1 
applozicConversationCreateTask = new ApplozicConversationCreateTask(context,conversationCreateListener,conversation);

To start the chat, execute the following code based on your desired action:

AlTask.execute(applozicConversationCreateTask);

Delete Chat Threads

Individual

If you want to delete an existing one to one chat thread, pass the RECEIVER_USERID in the below method and use context as this or application context to delete the one to one chat.

// Getting the contact object from RECEIVER_USERID
 AppContactService contactService = new AppContactService(context); 
 Contact contact = contactService.getContactById(<RECEIVER_USERID>);
 
// Calling user conversation delete task 
 AlTask.execute(new DeleteConversationAsyncTask(new MobiComConversationService(context), contact, null, null, context));

Group and Context

If you want to delete an existing group chat, pass the RECEIVER_USERID in the below method and use context as this or application context to delete the group chats.

// Getting the channel object from group id or channelKey 
Channel channel = ChannelService.getInstance(this).getChannel(groupId);

// Calling group conversation delete task 
AlTask.execute(new DeleteConversationAsyncTask(new MobiComConversationService(context), null, channel, null, context));

End of Tutorial 3

In this Applozic Android SDK tutorial, we have learnt how to:

  • Launch a chat conversation list screen
  • Create an individual chat thread
  • Create a group chat thread
  • Create a context based chat thread
  • Delete chat threads

You are now ready to move to Tutorial 4, Push Notifications!

Author

Anurag is a Technical Writer and technology enthusiast who enjoys science fiction and PC gaming.