Advanced intégration
MQTT Chat can be customized to best suit your application need.
MQTTChat listeners seen before allow developer to be able to carry out the adequate treatment generally after the action is executed. On the other hand, Events Callbacks allow certain actions to be authorized or not and therefore to make a treatment (local or server side) before deciding authorizing or refusing the execution of certan actions.
MQTTChat messenger uses this java class called
EventsCallbacks
to handle plugins restriction and customisation.public class EventsCallbacks {
public EventsCallbacks() {
}
public void onSendingMessage(int toUserId, Message message, AuthorisationCallback authorisationCallback) {
authorisationCallback.OK();
}
public void onRemovingFriend(int friendId, AuthorisationCallback authorisationCallback) {
authorisationCallback.OK();
}
public void onLockingUser(int userId, AuthorisationCallback authorisationCallback) {
authorisationCallback.OK();
}
public void onUnLockingUser(int userId, AuthorisationCallback authorisationCallback) {
authorisationCallback.OK();
}
public void onDeletingConversation(int userId, AuthorisationCallback authorisationCallback) {
authorisationCallback.OK();
}
public void onAudioCallClick(int userId, AuthorisationCallback authorisationCallback) {
authorisationCallback.OK();
}
public void onVideoCallClick(int userId, AuthorisationCallback authorisationCallback) {
authorisationCallback.OK();
}
}
And below the abstract
AuthorisationCallback
class.public abstract class AuthorisationCallback {
public AuthorisationCallback() {
}
public abstract void OK();
public abstract void Cancel(String var1);
}
As you can see, by default
EventsCallbacks
class does not apply any restriction when sending messages or establishing audio and video calls between users. This default behaviour can be customised to implement desired restruction that we want implement. For example, if you want to prohibit the sending of messages to a specific user (userid=10). You can create a custom class
CustomEventsCallbacks
which extends the parent class EventsCallbacks
. Then in onSendingMessage()
we make condition on toUserId
parameter. see code below:public class CustomEventsCallbacks extends EventsCallbacks {
private Context mContext;
public CustomEventsCallbacks(Context mContext) {
this.mContext = mContext;
}
public void onSendingMessage(int toUserId, Message message, AuthorisationCallback authorisationCallback) {
if(toUserId==10){
authorisationCallback.Cancel("You can not send message to this user");
}else{
authorisationCallback.OK();
}
}
}
Finally, d'ont forget to affect our custom class to MQTTChat messenger to override default
👍
EventsCallbacks
class like below:MqttChat.getInstanavace().setEventsCallbacks(new CustomEventsCallbacks(getApplicationContext()));
authorisationCallback.Cancel(String message)
;will interrupt the execution of the action and display the message to the user in the same time.

MQTTChat GUI already offers a basic interface to allow users to change chat settings.
If you want to use gloobal settings or integrate chat settings into your main settings activity. You can proceed as follows:
- 1.Remove settings button from GUI.
MqttChat.getInstance().getMySettings().setShowSettingsIcon(false);
2. Change MQTTChat Settings using Code.
- Enable/Disable sound notifications:
MqttChat.getInstance().getMySettings().setSoundAlerts(true);
MqttChat.getInstance().getMySettings().isSoundAlerts();
- Enable/Disable Audio calls:
MqttChat.getInstance().getMySettings().setAcOk(true);
MqttChat.getInstance().getMySettings().isAcOk()
- Enable/Disable Video calls:
MqttChat.getInstance().getMySettings().setVcOk(false);
MqttChat.getInstance().getMySettings().isVcOk()
- Enable/Disable FCM Notifications :
If true, an android notification will be displayed to the user if a message is received and the app is closed.
MqttChat.getInstance().getMySettings().setNotification(true);
MqttChat.getInstance().getMySettings().isNotifications();
3. Change user status programmatically.
Status can be :
Presence.ONLINE
or Presence.BUSY
. Second parameter can be : null
or Callback
object.MqttChat.getInstance().getMyPresence().setUserStatus(Presence.ONLINE, null);
Before we see how to retrieve and view MQTT notifications. Click here to learn how to send notifications from Server using REST API.
To retrieve MQTT notifications sent from the server to android Application two cases may arise:
In this case android application is on foreground and connection with the MQTT server is established.
MqttChat.getInstance().addMqttchatListener(new myMqttChatListener() {
@Override
public void onReceiveNotification(Notification notification) {
/** notification received here **/
}
});
If application is closed or application is in background. in this case notification cannot be sent by MQTT and then will be sent with FCM provided FCM feature is enabled.
MqttChat.getInstance().addMqttchatFCMListener(new MqttChatFCMListener() {
@Override
public void onReceiveFCMNotification(boolean notificationsSettingEnabled, int toUserId, Notification notification) {
/** notification received here **/
}
});
notificationsSettinEnabled
: indicates whether or not user accepts FCM notifications for offline messages in chat settings. If you want to consider it also to show notifications or you can simply ignore it. Therefore to receive notifications regardless of the application is in the foreground or closed. it is necessary to implement the two senarios.
Last modified 2mo ago