Ready UI

We are introducing Ready UI feature using which you can integrate a ready made chat UI into your app easily.

Use the following steps to integrate Ready UI in your app:

  • Create new module from File->New module
  • Select Import .JAR or .AAR package option
  • Select the cometchat-sdk.aar file and click finish
  • Go to File->Project Structure, add module dependency cometchat-sdk
  • Note that Ready UI only works with Android Studio and is available from SDK version 1.6.

    Add the following dependency to your build.gradle of your app and sync the Gradle

    compile 'com.google.code.gson:gson:2.3'
    compile 'com.pnikosis:materialish-progress:1.5'
    compile 'com.mcxiaoke.volley:library:1.0.17'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'org.jsoup:jsoup:1.8.1'
    compile 'org.apache.httpcomponents:httpcore:4.3.2'
    compile 'org.apache.httpcomponents:httpmime:4.3.3'
    compile 'com.parse.bolts:bolts-tasks:1.4.0'
    compile 'com.commit451:PhotoView:1.2.4'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.splitwise:tokenautocomplete:1.3.3'

    Add the following line in defaultConfig element present in your build.gradle file

     multiDexEnabled true 

    Add the following lines in android element present in your build.gradle file

    dexOptions {
            incremental true
            javaMaxHeapSize "2048M"
    }
    
    packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
    }

    Add the following permissions in your AndroidManifest.xml file

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.VIBRATE" />

    Create a class inside your package which will extends “CCApplication” and provide its reference in the manifest file as follows

    public class MYChatApplication extends CCApplication {
    
      ....
    }
    

    In your AndroidManifest.xml file

    <application
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme"
    android:name=".ChatApplication"
    ...
    >
    

    With Ready UI, you can launch a chat window of a specific user or launch the whole chat app inside your app.

    Set CometChat api-key
    You need to set the the api-key for your cloud subscription with setApiKey() function. The syntax for this function is as follows:

    setApiKey(Context context, String apikey)

    e.g

    MessageSDK.setApiKey(getApplicationContext(), "12345xabcdefghijklmnopqrstuvwxyz");

    Login to CometChat
    You can login to CometChat by 2 ways:
    1. Using username and password and
    2. Using userid only

    – Login using username and password
    Use the following syntax to pass username and password to the login() function.

    login(Context context, String username, String password, Callbacks callbacks)

    e.g

    MessageSDK.login(getApplicationContext(), "John","mypass0rd",  new LoginCallbacks() {
         @Override
         public void successCallback(JSONObject jsonObject) {
    
         }
         @Override
         public void failCallback(JSONObject jsonObject) {
    
         }
         @Override
         public void userInfoCallback(JSONObject jsonObject) {
    
         }
         @Override
         public void chatroomInfoCallback(JSONObject jsonObject) {
    
         }
         @Override
         public void onMessageReceive(JSONObject jsonObject) {
    
         }
    
         @Override
         public void chatWindowListner(JSONObject jsonObject) {
    
         }
    });

    If the login is successful, you will get {“success”:”login successful”} message as success response.

    – Login using userid
    The syntax for using this login mechanism is as follows:

    login(Context context, String userid,LoginCallbacks callbacks)

    e.g

    MessageSDK.login(getApplicationContext(), "18", new Callbacks() {
         @Override
         public void successCallback(JSONObject jsonObject) {
    
         }
         @Override
         public void failCallback(JSONObject jsonObject) {
    
         }
         @Override
         public void userInfoCallback(JSONObject jsonObject) {
    
         }
         @Override
         public void chatroomInfoCallback(JSONObject jsonObject) {
    
         }
         @Override
         public void onMessageReceive(JSONObject jsonObject) {
    
         }
    
         @Override
         public void chatWindowListner(JSONObject jsonObject) {
    
         }
    });

    The success response will be the same.

    Initialize CometChat
    Once you have logged in successfully, you need to initialize CometChat by using initializeCometChat() function. The syntax for this function is as follows:

    initializeCometChat(Context context)

    Launch chat window
    In order to open the chat window of a user, use launchChatWindow() function. The syntax for this function is as follows:

    launchChatWindow(boolean flag, Activity context, String userid, Callbacks callbacks)

    e.g

    MessageSDK.launchChatWindow(false, YourActivity.this, "16", new Callbacks() {
         @Override
         public void successCallback(JSONObject jsonObject) {
    
         }
         @Override
         public void failCallback(JSONObject jsonObject) {
    
         }
    });

    Description of parameters is as follows:

  • flag: Its a boolean flag. If set to true, then you can navigate back to userlist. If set to false, then you can only view the chat window
  • context: Pass the context of your activity/application
  • userid: Pass the userid of the user with whom you wish to chat
  • Launch CometChat
    You can open the ready to use chat app inside your app by using launchCometChat() function. The syntax for this function is as follows:

    launchCometChat(Activity context,Callbacks callbacks)

    e.g

    MessageSDK.launchCometChat(Youractivity.this, new Callbacks() {
         @Override
         public void successCallback(JSONObject jsonObject) {
    
         }
         @Override
         public void failCallback(JSONObject jsonObject) {
    
         }
    });

    Not finding what you need?

    The CometChat team is here to help!