Integration

MQTT chat android library that you can integrate in your android application from API 14.

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

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

android {
defaultConfig {
multiDexEnabled true
}
}
dependencies {
implementation 'com.telifoun.mqttchat:mqttchat-core:1.2.1'
implementation 'com.telifoun.mqttchat:mqttchat-gui:1.2.1'
}

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 log in logcat

Optional

Usage

Login user

Before you can display any chat interface in your application you must first login your user to MQTT chat to do this, In your LoginActivity and just after user login success to your application, You can log him to MQTT chat using logIn 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 s) {
}
});

You can then check whether a user is logged to MQTT chat or not using this code.

Mqttchat.getmInstance().getLoggedUser().isLogged();

Show Chat GUI

Once the user is logged 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 specific user

To open a chat window directly from your application when for example a user is viewing the profile of another user and wants to start a chat with him. You can then directly display the chat window by clicking on a button.

Mqttchat.getmInstance().openChatWindowWith(MainActivity.this, userid, new Callback() {
@Override
public void OK(Object o) {}
@Override
public void KO(String error) {}
});

Refresh user's contacts list

Mqttchat.getmInstance().refreshContactsList();

Logout user

This function is used when user disconnect from your application.

Mqttchat.getmInstance().logOut(new Callback() {
@Override
public void OK(Object o) {
}
@Override
public void KO(String s) {
}
});