Chat events
Events Litener.
Interaction with library is possible essentially through two listeners: MqttChatListener and MqttChatFCMListener.
MqttChatListener events : are called when application is in foreground or is still in task list.
MqttChatFCMListener events : are called by google play firebase services when application is closed and FCM feature is activated. Click here to see how to activate FCM in Mqttchat android library.

MQTTChat listener

You can capture all MQTT chat events using a MqttChatListener :
1
MqttChatListener listener= new MqttChatListener() {
2
3
@Override
4
public void onLoadComplete() {
5
Log.i(Tag,"MQTT Chat load complete");
6
}
7
8
@Override
9
public void onUserPresenceUpdate(int i, String s, boolean b, boolean b1, String s1) {
10
Log.i(Tag,"User presence update : userId:"+i +"; new status:"+s+" ;voice capacity:"+b +" ;video capacity:"+b1+"; timestamp:"+s1);
11
}
12
13
@Override
14
public void onSendMessage(int i, Message message) {
15
Log.i(Tag,"Logged user send message to userId :"+i +"; message:"+message.toString());
16
}
17
18
@Override
19
public void onAckMessage(int i, Ack ack, Message message) {
20
Log.i(Tag,"Logged user received Ack from server for message sent to userId :"+i +"; ack :"+ack.toString()+"; message:"+message.toString());
21
}
22
23
@Override
24
public void onRejectMessage(int i, String s, Message message) {
25
Log.i(Tag,"Logged user has sent message to userId:"+i+"; message was rejected by server because of :"+s+"; and message content :"+message.toString());
26
}
27
28
@Override
29
public void onIncomingMessage(int i, Message message) {
30
Log.i(Tag,"Logged user received new message from userId:"+i+"; message content:"+message.toString());
31
}
32
33
@Override
34
void onReceiveNotification(Notification notification){
35
Log.i(Tag,"Logged user received MQTT chat notification:"+notification.toString());
36
}
37
38
@Override
39
public void onUserTyping(int i, boolean b) {
40
Log.i(Tag,"A user userId:"+i+" is currently typing to logged user; is typing :"+b);
41
}
42
43
@Override
44
public void onReadingMessage(int i, int i1, String s) {
45
Log.i(Tag,"A user userId:"+i+"; is reading message id:"+i1+" ; reading date :"+s);
46
}
47
48
@Override
49
public void onNotReadMessagesCountUpdate(int i) {
50
Log.i(Tag,"Number of unread messages is changed for logged user, new number is :"+i);
51
}
52
53
@Override
54
void onRemoveFriend(int i){
55
Log.i(Tag,"User removed a friend with userid from "+i+"fiends list.");
56
}
57
58
@Override
59
void onLockUser(int i){
60
Log.i(Tag,"User locked a user with id:"+i);
61
}
62
63
@Override
64
void onUnLockUser(int userId){
65
Log.i(Tag,"User Unlocked a user with id:"+i);
66
}
67
68
69
@Override
70
void onDeleteConversation(int i){
71
Log.i(Tag,"User deleted conversation with user id:"+i);
72
}
73
74
75
@Override
76
public void onError(int i, String s) {
77
Log.e(Tag,"MQTTCHAT error code :"+i+";message :"+s);
78
}
79
};
Copied!
To add listener to MQTT chat:
1
Mqttchat.getmInstance().addMqttchatListener(listener);
Copied!
And to remove listener :
1
Mqttchat.getmInstance().removeMqttchatListener(listener);
Copied!
You can add many listeners in differents activities or fragments. So you can catch events where you need.
Event
Description
void onLoadComplete();
When chat is completely loaded.
void onUserPresenceUpdate(int userId, String status, boolean audioCap, boolean videoCAP, String timestamp);
When a contact in contacts list or friend user in friends list has changed status.
void onSendMessage(int toUserId, Message message);
When logged user send a message.
void onAckMessage(int toUserId, Ack ack,Message message);
When ACK is received from server. that mean that the message is successfully sent and will be delivered.
void onRejectMessage(int toUserId,String why,Message message);
When message is rejected from server. because user is locked by toUserid user.
void onIncomingMessage(int fromUserId,Message message);
When logged user receive new message.
void onReceiveNotification(Notification notification);
When logged user receive new Notification.
void onUserTyping(int fromUserId,boolean on);
When a contact or friend is typing.
void onReadingMessage(int fromUserId,int messageId,String readDate);
When a contact or friend read a sent message.
void onNotReadMessagesCountUpdate(int n);
When a number or unreaded message change.
void onRemoveFriend(int userId);
When logged user remove a friend from friends list.
void onLockUser(int userId);
When logged user lock a contact or friend.
void onUnLockUser(int userId);
When logged user unlock a contact or friend.
void onDeleteConversation(int userId);
When logged delete conversation with a contact.
void onError(int code,String error);
When error occured.

MQTTChat FCM listener

Two types of FCM events are provided by the listener: events following an MQTT Chat notification received. Or else an FCM notification message not sent by MQTT Chat but received by FCM service. It can be sent by your server or another library.
1
Mqttchat.getmInstance().addMqttchatFCMListener(new MqttChatFCMListener() {
2
@Override
3
public void onReceiveFCMNotification(boolean notificationsSettingEnabled, int toUserId, Notification notification) {
4
5
}
6
7
@Override
8
public void onReceiveOtherFCMData(com.google.firebase.messaging.RemoteMessage message) {
9
10
}
11
});
12
Copied!
Event
Description
void onReceiveFCMNotification(boolean notificationsSettingEnabled, int toUserId, Notification notification);
When MQTT Chat server send notification to FCM. That means logged user received a notification when application is closed.
void onReceiveOtherFCMData(RemoteMessage message);
When an FCM notification message not sent by MQTT Chat is received by FCM service and then delivered to you through this event.
For more details about MQTT advanced notifications management in android library please see this section.
Last modified 24d ago