MQTTchat Android
MQTT chat Android is an android library that provides full chat functionality and can be integrated into any android application from API 16.
- Go to the Settings tab of your admin panel to get your
APP_ID
andAPP_SECRET
. You may need these during installation.
- 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 dataBinding then add MQTT chat artifacts as a dependencies in the
build.gradle
file of your main app:
android {
compileSdkVersion 31
defaultConfig {
multiDexEnabled true
}
buildFeatures{
dataBinding true
}
....
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.telifoun.mqttchat:mqttchat-core:4.2.0'
implementation 'com.telifoun.mqttchat:mqttchat-gui:4.2.0'
}
In
Application
class of your android app add the following code to init MQTT chat:new MqttChat.getBuilder()
.context(this.getApplicationContext())
.appIcon(R.drawable.ic_launcher)
.domain("your_domain.com")
.appId("APP_ID")
.appSecret("APP_SECRET")
.debugMode(false)
.useFriends(false)
.build();
In
LoginActivity
after user login success to your application, use logIn()
function to login user to MQTT chat.MqttChat.getInstance().logIn(getApplication(), userId, new CallbackListener() {
@Override
public void onSuccess(Object o) {
}
@Override
public void onError(String s) {
}
});
Connect()
function allows you to connect the logged-in user to MQTT chat. A user should be logged and connected to MQTT chat to be able to view chat interfaces.MqttChat.getInstance().Connect(new CallbackListener() {
@Override
public void onSuccess(Object o) {
}
@Override
public void onError(String error) {
}
});
Once the user is connected 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=".gui.ui.fragments.mqttchat.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);
getSupportFragmentManager().beginTransaction().replace(R.id.mqttchatFragment,new MqttChatFragment(), "mqttchat").commit();
}
}
MainActivity
should extend PresenceActivityA
to handle MQTT reconnections and states of presence transitions.Last modified 5mo ago