Talk to us
Talk to us
menu

How to Create App Chat in Easy Steps

<strong>How to Create App Chat in Easy Steps</strong>

People and even businesses now use app chats for various purposes. For instance, many businesses now use app chats like Slack for quick information transmission, and for even meetings and collaboration. In this article, we’ll learn what app chats are and how to build them using ZEGOCLOUD chat API.

What is App Chat?

A chat app allows you to communicate with people all over the world in real time by sending and receiving messages.

Custom messaging features enable users of a web or mobile chat app to receive the same engaging and lively interactions as they would in person. This also keeps users conversing on your platform rather than searching for another messaging solution. Including personalized chat features in your app, whether private chat, group chat, or large-scale chat, can help ensure that your users have an enjoyable experience.

In-app chat apps can be integrated into a variety of apps to provide streamlined and highly interactive communication. App chat is now common in many apps and websites, owing to the increasing use of chatbots for customer service and expert systems.

How to Build App Chat with ZEGOCLOUD API/SDK

In-app chat is a useful feature to have in a mobile app. That being said, in the following section, we’ll use ZEGOCLOUD’s In-app chat SDK (i.e. Zim SDK) to create app chat functionality.

zegocloud in app chat

ZEGOCLOUD in-app chat SDK provides a communication channel with high reliability, high concurrency, and very low latency for interactive messaging. Real-time messaging can be set up in minutes for things like large-scale streaming, live audio rooms, an online chat system, and more.

When developing apps that require live chat functionality, there are numerous reasons why you should use ZEGOCLOUD In-app chat SDK.

The In-app chat SDK includes the following fantastic features:

1. Increased performance in slow networks

The in-app chat SDK is designed to provide smooth communication and message transmission even in poor network conditions. Even when network conditions result in a 90% packet loss rate, messages are reliably delivered. So you don’t have to worry about dealing with network issues because the SDK in live chat for Android apps handles them out of the box.

2. Message reachability

This is one of ZEGOCLOUD’s most powerful in-app chat SDK features. Message priorities can be set using this SDK. Setting the message deliverability priority to high ensures that no important message is ever left hanging.

3. International reach with ultra-low latency

The real-time communication network of ZEGOCLOUD spans over 200 countries and regions. It delivers real-time messages to users with as little as 200 ms latency thanks to an excellent global network scheduling strategy.

4. Extensive concurrency

The in-app chat of ZEGOCLOUD can easily handle hundreds of millions of concurrent messages, meeting the needs of large-scale and highly demanding live communication events.

Preparation

  • A developer account on ZEGOCLOUD — Register now
  • Make a project, obtain the AppID, and obtain the AppSign.
  • Android Studio 2.1 or later is required.
  • an Android device or emulator that supports video and audio
  • Basic understanding of Android app development

ZIM Chat SDK Integration

So far, we’ve discussed what the in-app chat SDK is and how it can help us easily integrate chat functionality into our app. This section will teach you how to make a chat app with Android Studio and the In-App Chat SDK.

To integrate the In-App Chat SDK into your project, follow the steps below:

Create a new project.

  1. Launch Android Studio and choose File → New Project.
  1. Enter the name of the application and select the project location.
  1. It is best to leave all other settings at their defaults. Click “Next”, followed by “Finish”.

Import the SDK

  1. Get the most recent version of the SDK from SDK downloads.
  2. Place SDK files in your project directory, such as app/libs.
  3. Open app/build.gradle and add the following content:
  • To specify the supported ABIs, add the ndk node inside the defaultConfig node.
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64', 'x86' 
}
  • Inside the android node, add the sourceSets node to specify the directory containing the SDK files.
sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
    }
}
  • In the dependencies node, paste the following code:
implementation fileTree(dir: 'libs', include: ['*.jar'])

Adding permissions

We require permission to access some Android operating system resources. For example, we need permission to use the camera and microphone.

This permission can be granted by following the steps outlined below:

  1. Open the AndroidManifest.xml file in the app/src/main directory and add the following code:
<!-- Permissions required by the SDK -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  1. Avoid obfuscation of class names by including the following codes in proguard-rules.pro:
-keep class **.zego.**{*;}

Implementation steps

To create a basic in-app Android chat app, follow the steps below:

Import class file.

import im.zego.zim.ZIM

Initialize Zim SDK.

The first step is to set up a ZIM instance. An instance is the same as a user logging into the system as a client.

Assuming we have two clients, A and B, who want to send and receive messages from and to each other, both of them will need to call the create method with the AppID in the previous prerequisite steps to create their own ZIM SDK instance:

// Create a ZIM SDK object and pass the AppID, AppSign, and Application in Android.
ZIMAppConfig appConfig = new ZIMAppConfig();
appConfig.appID = 12345;  
appConfig.appSign = "appSign";  
zim = ZIM.create(appConfig, application);

Create a handler object for an event.

Before starting the user’s login, you must call the setEventHandler method to create an event handler object and customize the event callbacks so that you can receive callback notifications when SDK errors occur or message-related callback notifications.

zim.setEventHandler(new ZIMEventHandler() {
    @Override
    public void onReceivePeerMessage(ZIM zim, ArrayList<ZIMMessage> messageList, String fromUserID) {
        // Implement the callback for receiving the one-to-one messages.
    }
});

Log in to ZIM SDK.

Clients A and B must log in to the ZIM SDK after creating the ZIM SDK instance in order to send and receive messages.

To access the ZIM SDK, follow the steps below:

  1. To create a user object, use the ZIMUserInfo method.
  2. Then, using their own user information, call the login method.
// userID and userName must be within 32 bytes, and can only contain numbers, letters and the following special characters: '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '=', '-', '`', ';', ''', ',', '.', '<', '>', '/', '\'。
ZIMUserInfo zimUserInfo = new ZIMUserInfo();
zimUserInfo.userID = userID;
zimUserInfo.userName = userName;
zim.login(zimUserInfo, new ZIMLoggedInCallback() {
    @Override
    public void onLoggedIn(ZIMError error) {
          // You can know whether the login is successful according to the ZIMError.           
    }
 });

Send one-to-one messages.

A and B can easily send messages to each other after logging into the SDK by invoking the sendPeerMessage method with client B’s userID, message content, and other information. The onMessageSent callback can be used to determine the status of a sent message. A will send a message to B in this scenario.

// Send one-to-one messages. 

String toUserID = "xxxx";

ZIMTextMessage zimMessage = new ZIMTextMessage();
zimMessage.message = "Message content";

ZIMMessageSendConfig config = new ZIMMessageSendConfig();
// Set message priority. 1: Low (by default). 2: Medium. 3: High.
config.priority = ZIMMessagePriority.LOW;
// Set up the configuration of offline notification.
ZIMPushConfig pushConfig = new ZIMPushConfig();
pushConfig.title = "Offline notification title";
pushConfig.content= "Offline notification content";
pushConfig.extendedData = "Extend information of the offline notification";
config.pushConfig = pushConfig;

zim.sendPeerMessage(zimMessage, toUserID, config, new ZIMMessageSentCallback() {
    @Override
    public void onMessageSent(ZIMMessage zimMessage, ZIMError error) {
        //  You can implement the event callback for sending messages here.
    }
});

Receive one-to-one messages.

After logging in, Client B can now receive the message from Client A via the callback onReceivePeerMessage, which is already set in the setEventHandler method.

zim.setEventHandler(new ZIMEventHandler() {
    @Override
    public void onReceivePeerMessage(ZIM zim, ArrayList<ZIMMessage> messageList, String fromUserID) {

      for (ZIMMessage zimMessage : messageList) {
          if (zimMessage instanceof ZIMTextMessage) 
          {
            ZIMTextMessage zimTextMessage = (ZIMTextMessage) zimMessage;
            Log.e(TAG, "Received message:"+ zimTextMessage.message);
          }   
      }
   }
});

Log out

Simply calling the logout method will log you out of the ZIM SDK.

zim.logout()

Destroy the ZIM SDK instance.

Call the destroy method to destroy the ZIM SDK instance.

zim.destroy();

Run a demo

You can get this article’s demo source code by downloading it.

Conclusion

Putting chat features into an app has never been easy. The ZIM SDK makes it easy to do that in a short amount of time. Why start from scratch when you can just use a strong, reliable, and full-featured SDK that has all the features you’ve ever needed?

Read More

Talk to Expert

Learn more about our solutions and get your question answered.

Talk to us

Take your apps to the next level with our voice, video and chat APIs

Free Trial
  • 10,000 minutes for free
  • 4,000+ corporate clients
  • 3 Billion daily call minutes

Stay updated with us by signing up for our newsletter!

Don't miss out on important news and updates from ZEGOCLOUD!

* You may unsubscribe at any time using the unsubscribe link in the digest email. See our privacy policy for more information.