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
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.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:support-v4:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.google.code.gson:gson:2.3'
compile 'com.google.firebase:firebase-messaging:9.0.0'
compile 'com.mcxiaoke.volley:library:1.0.17'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.github.clans:fab:1.6.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'
compile 'com.tubb.smrv:swipemenu-recyclerview:5.2.1'
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'se.emilsjolander:stickylistheaders:2.7.0''
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.CHANGE_NETWORK_STATE" />
<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.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_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" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Create a class inside your package which will extend “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 URL
Set the CometChat URL with setUrl() function. The syntax for this function is as follows:
setUrl(Context context, String siteurl, Callbacks callbacks)
Login to CometChat
You can login to CometChat by 2 ways:
1. Using username and password
2. Using userid only
– Login using username and password
Use the following syntax to pass username and password to login() function.
login(Context context, String username, String password, LoginCallbacks logincallbacks)
e.g
MessageSDK.login(getApplicationContext(), "John", "mypass0rd", new LoginCallbacks() {
@Override
public void successCallback(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) {
}
@Override
public void failCallback(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, Callbacks callbacks)
e.g
MessageSDK.login(getApplicationContext(), "18", new LoginCallbacks() {
@Override
public void successCallback(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) {
}
@Override
public void failCallback(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:
Launch CometChat
You can open 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!