Integration

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

Importing the Library

Add MQTT chat Artifactory repository to the list of Maven repositories in the top level build.gradle file of your project:
allprojects {
repositories {
maven {
url "https://mqttchat.jfrog.io/artifactory/libs-release-local"
credentials {
username = "mqttchat"
password = "telifoun"
}
}
}
}
Then simply set compiledSdkVersion to 31 at least, enable multidex and dataBinding then add MQTT chat artifacts as a dependencies in the build.gradle file of your main app.
android {
compileSdkVersion 31
defaultConfig {
multiDexEnabled true
}
buildFeatures{
dataBinding true
}
....
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.telifoun.mqttchat:mqttchat-core:4.2.0'
implementation 'com.telifoun.mqttchat:mqttchat-gui:4.2.0'
}

Initialisation

In Application class of your android app add the following code to init MQTT hat: You get your own APP_ID and APP_SECRET after registration to admin Panel.
new MqttChat.getBuilder()
.context(this.getApplicationContext())
.appIcon(R.drawable.ic_launcher)
.domain("your_domain.com")
.appId(App_Id)
.appSecret(App_Secret)
.debugMode(true)
.build();
Below all possible parameters for initializing MQTT chat.
Parameter
Description
Note
context
Application context
Mandatory
domain
Your domain
Mandatory
appId
MQTT chat APP_ID
Mandatory
appSecret
MQTT chat APP_SECRET
Mandatory
useFriends
Enable or not useFriends option
Optional
mqttServer
MQTT Server IP
Optional
mqttPort
MQTT Port
Optional
idleTimeout
Idle timeout to go to away status
Optional
AppIcon
Application Icon
Optional
debugMode
Enable or not debug mode to show logs in logcat
Optional

Usage

Login user

loginIn() function must be called in your LoginActivity just after user login success to your application. It allows to save the user id in a file and therefore you d'ont need to login the user each time you launch application. loginIn() function can be executed offline and the saved user id can only be cleared by executing logOut() function.
userId is the primary field value of your users table in application database.
MqttChat.getInstance().logIn(getApplication(), userId, new CallbackListener() {
@Override
public void onSuccess(Object o) {
}
@Override
public void onError(String s) {
}
});
You can then check whether a user is logged to MQTT chat or not using this code. That check if there is a saved userid in file.
MqttChat.getInstance().getLoggedUser().isLogged();

Connect user

This function allows you to connect the user to MQTT chat. It must be executed on application startup and before chowing chat interfaces. A user then must be logged and connected to MQTT chat before he can chat.
MqttChat.getInstance().Connect(new CallbackListener() {
@Override
public void onSuccess(Object o) {
}
@Override
public void onError(String error) {
}
});

Show Chat GUI

Once the user is connected to MQTT chat you can then display chat interface by embedding MQTT chat fragment into main activity or even into another fragment. This allows more flexibility and consistency in the design of the application. To do this.
  • First add FrameLayout component to MainActivity layout activity_layout.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:name=".gui.ui.fragments.mqttchat.MqttChatFragment"
android:id="@+id/mqttchatFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
  • Then in the oncreate() method of your main activity, affect MqttchatFragment to FrameLayout component.
public class MainActivity extends PresenceActivityA {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
getSupportFragmentManager().beginTransaction().replace(R.id.mqttchatFragment,new MqttChatFragment(), "mqttchat").commit();
}
}
Every activity used in your application should extends abstract class PresenceActivityA to keep presence system running in all activities of you Application. Except for activities called when user is not yet logged to MQTT chat like Register or Login activities.

Start Chat with userid

Sometimes you need to launch chat window directly from your application when for example a user is viewing another user's profile. Without of cource displaying the main chat interface. This is possible using startChatWith() function.
MqttChat.getInstance().startChatWithUser(Context ctx, int userId,CallbackListener clb) {
@Override
public void onSuccess(Object o) {
}
@Override
public void onError(String error) {
}
});

Logout user

This function is used when user log out from your application.
MqttChat.getInstance().logOut(new CallbackListener() {
@Override
public void onSuccess(Object o) {
}
@Override
public void onError(String error) {
}
});

Debug

Below differents tags used by MQTT chat for debugging:
Tag
Purpose
mqttchat.sdk
MQTT chat rest calls module
mqttchat.core.presence
MQTT chat presence management module
mqttchat.core.fcm
MQTT chat google FCM module
mqttchat.core.mqtt
MQTT client module
mqttcha.core.sync
MQTT contacts and messages sync
mqttchat.modules.visio
MQTT chat visio module.