MQTTchat Android
MQTT chat Android is an android library that provides full chat functionality and can be integrated into any android application from API 16.

Before you begin

  • Register to mqttchat, add your domain name and activate a subscription that suits you.
  • Go to the Settings tab of your admin panel to get your APP_ID and APP_SECRET. You may need these during installation.

Integration

Importing the Library

  • Add MQTT chat Artifactory repository to the list of Maven repositories in the top level build.gradle file of your project:
1
allprojects {
2
repositories {
3
maven {
4
url "https://mqttchat.jfrog.io/artifactory/libs-release-local"
5
credentials {
6
username = "mqttchat"
7
password = "telifoun"
8
}
9
}
10
}
11
}
Copied!
  • Then simply enable multidex and dataBinding then add MQTT chat artifacts as a dependencies in the build.gradle file of your main app:
1
android {
2
3
compileSdkVersion 31
4
defaultConfig {
5
multiDexEnabled true
6
}
7
buildFeatures{
8
dataBinding true
9
}
10
11
....
12
13
compileOptions {
14
sourceCompatibility JavaVersion.VERSION_1_8
15
targetCompatibility JavaVersion.VERSION_1_8
16
}
17
}
18
19
dependencies {
20
implementation 'com.telifoun.mqttchat:mqttchat-core:4.0.0'
21
implementation 'com.telifoun.mqttchat:mqttchat-gui:4.0.0'
22
}
Copied!

Initialisation

In Application class of your android app add the following code to init MQTT chat:
1
new MqttChat.getBuilder()
2
.context(this.getApplicationContext())
3
.appIcon(R.drawable.ic_launcher)
4
.domain("your_domain.com")
5
.appId("APP_ID")
6
.appSecret("APP_SECRET")
7
.debugMode(false)
8
.useFriends(false)
9
.build();
Copied!

User LogIn

In LoginActivity after user login success to your application, use logIn() function to login user to MQTT chat.
1
MqttChat.getInstance().logIn(getApplication(), userId, new CallbackListener() {
2
@Override
3
public void onSuccess(Object o) {
4
}
5
@Override
6
public void onError(String s) {
7
}
8
});
Copied!
User should be already added to MQTT chat using MQTTChat SDKs or by calling REST API.
😉

User Connect

Connect() function allows you to connect the logged-in user to MQTT chat. A user should be logged and connected to MQTT chat to be able to view chat interfaces.
1
MqttChat.getInstance().Connect(new CallbackListener() {
2
@Override
3
public void onSuccess(Object o) {
4
}
5
@Override
6
public void onError(String error) {
7
}
8
});
Copied!

Lounch Chat

Once the user is connected to MQTT chat, you can then display chat interface by embedding MQTT chat fragment into your main activity or into another fragment. To do this.
  1. 1.
    First add FrameLayout component to MainActivity layout activity_layout.xml.
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
android:layout_width="match_parent"
4
android:layout_height="match_parent"
5
android:orientation="vertical" >
6
7
<FrameLayout
8
android:name=".gui.ui.fragments.mqttchat.MqttChatFragment"
9
android:id="@+id/mqttchatFragment"
10
android:layout_width="match_parent"
11
android:layout_height="match_parent"/>
12
13
</LinearLayout>
Copied!
2. Then in the oncreate() method of your main activity, affect MqttchatFragment to FrameLayout component.
1
public class MainActivity extends PresenceActivityA {
2
3
@Override
4
protected void onCreate(Bundle savedInstanceState) {
5
super.onCreate(savedInstanceState);
6
setContentView(R.layout.activity_layout);
7
getSupportFragmentManager().beginTransaction().replace(R.id.mqttchatFragment,new MqttChatFragment(), "mqttchat").commit();
8
}
9
}
Copied!
MainActivity should extend PresenceActivityA to handle MQTT reconnections and states of presence transitions.
That is all. For advanced integration, please consult the complete MQTTchat Android documentation.
You can also read a step by step tutorial that explain MQTT chat Android Integration.