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:
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 set compiledSdkVersion to 30 at least, enable multidex and add MQTT chat artifacts as a dependencies in the build.gradle file of your main app.
1
android {
2
compileSdkVersion 30
3
defaultConfig {
4
multiDexEnabled true
5
}
6
}
7
8
dependencies {
9
implementation 'com.telifoun.mqttchat:mqttchat-core:3.2.0'
10
implementation 'com.telifoun.mqttchat:mqttchat-gui:3.2.0'
11
}
Copied!

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.
1
new Mqttchat.getBuilder()
2
.context(this.getApplicationContext())
3
.appName("Application Name")
4
.appIcon(R.drawable.ic_launcher)
5
.domain("your_domain.com")
6
.appId(App_Id)
7
.appSecret(App_Secret)
8
.debugMode(true)
9
.build();
Copied!
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
AppName
Application Name
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.
1
Mqttchat.getmInstance().logIn(getApplication(), userId, new Callback() {
2
@Override
3
public void OK(Object o) {
4
}
5
@Override
6
public void KO(String error) {
7
}
8
});
Copied!
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.
1
Mqttchat.getmInstance().getLoggedUser().isLogged();
Copied!

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.
1
Mqttchat.getmInstance().Connect(new Callback() {
2
@Override
3
public void OK(Object o) {
4
}
5
@Override
6
public void KO(String error) {
7
}
8
});
Copied!

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.
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="com.telifoun.mqttchat.gui.MqttchatFragment"
9
android:id="@+id/mqttchatFragment"
10
android:layout_width="match_parent"
11
android:layout_height="match_parent" />
12
13
</LinearLayout>
Copied!
  • 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
if(savedInstanceState==null){
8
getSupportFragmentManager().beginTransaction().add(R.id.mqttchatFragment,MqttchatFragment.newInstance(), "mqttchat").commit();
9
}
10
}
11
}
Copied!
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.
1
Mqttchat.getmInstance().startChatWith(Context ctx, int userId, Callback clb) {
2
@Override
3
public void OK(Object o) {
4
}
5
@Override
6
public void KO(String error) {
7
}
8
});
Copied!

Logout user

This function is used when user log out from your application.
1
Mqttchat.getmInstance().logOut(new Callback() {
2
@Override
3
public void OK(Object o) {
4
}
5
@Override
6
public void KO(String error) {
7
}
8
});
Copied!

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.
Last modified 2mo ago