Integration

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

MQTTchat 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 MQTTchat Artifactory repository to the list of Maven repositories in your top level build.gradle file:

allprojects {
repositories {
maven {
url "https://mqttchat.jfrog.io/artifactory/libs-release-local"
credentials {
username = "mqttchat"
password = "telifoun"
}
}
}
}

You can then simply add MQTTchat artifacts as a dependencies in the build.gradle file of your main project:

dependencies {
implementation 'com.telifoun.mqttchat:mqttchat-core:1.1.0'
implementation 'com.telifoun.mqttchat:mqttchat-gui:1.1.0'
}

Initialisation

In Application class of your android app add the following code to init MQTTchat: 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)
.showBackButton(true)
.debugMode(true)
.build();

Below all possible parameters for initializing MQTTchat.

Parameter

Description

Note

context

Application context

Mandatory

domain

Your domain

Mandatory

appId

MQTTchat APP_ID

Mandatory

appSecret

MQTTchat 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

showBackButton

Show or not back button in main GUI.

Optional

Usage

Login user

Before you can display any chat interface in your application you must first login your user to MQTTchat to do this, In your LoginActivity and just after user login success to your application, You can connect him to MQTTchat 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 MQTTchat or not using this code.

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

Show Chat GUIs

Once the user is connected to MQTTchat you can then display chat interface by choosing the option that suits you:

Launch a chat Activity

The easiest and fastest way to display chat GUI to the user is to start chat as separate Activity, when for example the client clicks on a button or a menu icon.

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

Lounch chat as a Fragment

In some cases, developers want to integrate chat as a fragment to embed it into the main activity or even into another fragment. This allows more flexibility and consistency in the design of the application. We have offered this possibility with the greatest possible ease.

  • 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);
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 MQTTchat like Register or Login Activities.

Be carefull: Extending PresenceActivityA for activities when user is not logged to Mqttchat will raise error.

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) {}
});

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) {
}
});