DEV Community

Cover image for Build an Encrypted chatting app with Java
Rajjan Sharma
Rajjan Sharma

Posted on • Updated on

Build an Encrypted chatting app with Java

In this article, we will learn how can we Integrate In-app Chat in android app with few lines of code.Let's suppose you have a startup and you don't have a team to work on your technical background , so you have to manage everything by your own , so this can be very stressful and exhausting. So guys the solution of this problem is ZegoCloud its an audio and video API provider which offers you so many exciting features and SDk that you can integrate inside your application and the best part is , managing your backend is quite easy if you are using ZEGOCLOUD.

In modern applications , chatting is a very important feature that users want , so they can communicate with each other . When you create this type of feature so you have to manage everything , here ZEGOCLOUD comes to make it possible and easy .

The best part is, whatever I am showing in this tutorial can be integrated without paying anything , just visit their website and create an account and you are ready to go >>

How to integrate In-app chatting functionality in android app.

  1. Once you will create account so you will see Dashboard , where you have to click on project >>> Create .

Image description

Then you must specify use case we want to use , in this we will cover In-app chat.

Then you must add a name .

Then we have to two options to choose , Uikits and SDK .The advantage of UI kits is , it have everything you need to implement.
Image description

What is the In-app Chat Kit
In-app Chat Kit is a UI component library based on the In-app Chat SDK. It provides general UI components, such as the chat list, one-on-one chat, and group chat. You can use the In-app Chat Kit to quickly build custom IM applications based on actual business requirements.

You have to select any one option for proceeding, Once project is created .
we will choose "for android" option.

Now click on "Save & Start to integrate":

After the project creation we need two values AppSign and AppId for connecting our app with zego porject.

Building In-app chat

Now open your IDE in which you wanna work , I will be using Android studio.

So create a project in android studio.

Add this dependency inside build.gradle level

android {
   ...
   buildFeatures {
        dataBinding true
    }
}

dependencies {
    ...
    implementation 'com.github.ZEGOCLOUD:zego_inapp_chat_uikit_android:+'    // add this line in your module-level build.gradle file's dependencies, usually named [app].
}
Enter fullscreen mode Exit fullscreen mode

Open build.gradle (module level) add these line of code.


android {
    buildToolsVersion "30.0.3"
    compileSdkVersion 31
    defaultConfig {
        applicationId "im.zego.zimexample"
        targetSdkVersion 31
        minSdk 24

        versionCode 1
        versionName "1.0"

        ndk {
            abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
        }
    }

Enter fullscreen mode Exit fullscreen mode

Add these lines in build.gradle as well

buildscript {
    repositories {
        maven { url 'https://www.jitpack.io' } //Add this line.
        ....
    }
}

allprojects {
    repositories {
        maven { url 'https://www.jitpack.io' }//Add this line.
        ...
    }
}
Enter fullscreen mode Exit fullscreen mode

Check your project root directory's gradle.properties file, and add the following configuration to the file if it's missing:

// Indicates AndroidX is enabled for the current project.
android.useAndroidX=true
// Migrates the dependency to AndroidX, converting the support library from third-party libraries used in the project to an AndroidX library.
android.enableJetifier=true
Enter fullscreen mode Exit fullscreen mode

Then we must add permissions that we need in order to make the app working ,
open manifest tag , inside Android manifest.xml .

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_LOGS" />
Enter fullscreen mode Exit fullscreen mode

Integrate In-app Chat Kit into the project.

Now we will call init method to intialize In-app chat kit.


import android.app.Application;

import com.zegocloud.zimkit.services.ZIMKit;

public class MyApplication extends Application {
    public static MyApplication sInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        Long appId = ;    // The AppID you get from ZEGOCLOUD Admin Console.
        String appSign = ;    // The App Sign you get from ZEGOCLOUD Admin Console.
        ZIMKit.initWith(this,appId,appSign);
        // Online notification for the initialization (use the following code if this is needed).
        ZIMKit.initNotifications();
    }
}
Enter fullscreen mode Exit fullscreen mode

After intialization , call the connectUser method with your user info to log in to In-app Chat Kit.


import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import im.zego.zim.enums.ZIMErrorCode;
import com.zegocloud.zimkit.services.ZIMKit;

public class MyZIMKitActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void buttonClick() {
        // userId and userName: 1 to 32 characters, can only contain digits, letters, and the following special characters: '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '=', '-', '`', ';', '’', ',', '.', '<', '>', '/', '\'
        String userId = ; // Your ID as a user.
        String userName = ; // You name as a user.
        String userAvatar = ; // The image you set as the user avatar must be network image. e.g., https://storage.zego.im/IMKit/avatar/avatar-0.png
        connectUser(userId, userName,userAvatar); 
    }

    public void connectUser(String userId, String userName,String userAvatar) {
        // Logs in.
        ZIMKit.connectUser(userId,userName,userAvatar, errorInfo -> {
            if (errorInfo.code == ZIMErrorCode.SUCCESS) {
                // Operation after successful login. You will be redirected to other modules only after successful login. In this sample code, you will be redirected to the conversation module.
                toConversationActivity();
            } else {

            }
        });
    }

    // Integrate the conversation list into your Activity as a Fragment 
    private void toConversationActivity() {
      // Redirect to the conversation list (Activity) you created. 
      Intent intent = new Intent(this,ConversationActivity.class);
      startActivity(intent);
    }
}
Enter fullscreen mode Exit fullscreen mode

Now we will display conversation component.

 public class ConversationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation);
    }

}
Enter fullscreen mode Exit fullscreen mode

The layout of the ConversationActivity is activity_conversation.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/frag_conversation_list"
        android:name="com.zegocloud.zimkit.components.conversation.ui.ZIMKitConversationFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Enter fullscreen mode Exit fullscreen mode

By this point your app will look like this-

Image description

Now we will focus on one-to-one chat -
Get the userId that is generated using your own business logic. (the userId here refers to the peer user you want to chat with.)
Fill in the userId parameter and run the following code:

  private void startSingleChat(String userId){
         ZIMKitRouter.toMessageActivity(this, userId,ZIMKitConversationType.ZIMKitConversationTypePeer);
    }
Enter fullscreen mode Exit fullscreen mode

Conclusion
So guys you can see that Building a chat app is so easy with ZegoCloud In-app chat SDK Thanks to ZEGOCLOUD for sponsoring this article, don't forget to visit their website to get more information about their products and services.

Top comments (0)