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 30 at least, enable multidex and add MQTT chat artifacts as a dependencies in the build.gradle file of your main app.

android {
compileSdkVersion 30
defaultConfig {
multiDexEnabled true
}
}
dependencies {
implementation 'com.telifoun.mqttchat:mqttchat-core:3.1.0'
implementation 'com.telifoun.mqttchat:mqttchat-gui:3.1.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())
.appName("Application Name")
.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

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.

Mqttchat.getmInstance().logIn(getApplication(), userId, new Callback() {
@Override
public void OK(Object o) {
}
@Override
public void KO(String error) {
}
});

You can then check whether a user is logged to MQTT chat or not using this code. That check if there is a saved user id in file.

Mqttchat.getmInstance().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.getmInstance().Connect(new Callback() {
@Override
public void OK(Object o) {
}
@Override
public void KO(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="com.telifoun.mqttchat.gui.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);
if(savedInstanceState==null){
getSupportFragmentManager().beginTransaction().add(R.id.mqttchatFragment,MqttchatFragment.newInstance(), "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.getmInstance().startChatWith(Context ctx, int userId, Callback clb) {
@Override
public void OK(Object o) {
}
@Override
public void KO(String error) {
}
});

Logout user

This function is used when user log out from your application.

Mqttchat.getmInstance().logOut(new Callback() {
@Override
public void OK(Object o) {
}
@Override
public void KO(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.