MQTTchat Android

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

Before you begin

  • Register to mqttchat, add your domain name and activate a subscription that suits you.

  • Go to the Settings tab of your admin panel to get your APP_ID and APP_SECRET. You may need these during installation.

Integration

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 chat:

new Mqttchat.getBuilder()
.context(this.getApplicationContext())
.appName(getApplicationContext().getResources().getString(R.string.app_name))
.appIcon(R.drawable.ic_launcher)
.domain("your_domain.com")
.appId("APP_ID")
.appSecret("APP_SECRET")
.debugMode(false)
.useFriends(false)
.build();

User LogIn

In LoginActivity after user login success, login user to MQTT chat using logIn() function.

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

User should be already added to MQTT Chat using MQTTChat SDKs or by calling REST API. 😉

Lounch Chat

Once the user is logged to MQTT chat you can then display chat interface by embedding MQTT chat fragment into your main activity or into another fragment. To do this.

  1. 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>

2. 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();
}
}
}

That is all. For advanced integration, please consult the complete MQTTchat Android documentation.

You can also read a step by step tutorial that explain MQTT chat Android Integration.