Push notification

Prerequisites#

Your android app should have a firebase cloud messaging integration.

Updating fcm device token#

You need to update the fcm device token to the server, so that server can send push notificationns by using the updated fcm device token. if you want to update the fcm device token, you can utilise the below method.

ArgumentTypeDescription
FCM_TOKENStringfcm device token
CALLBACKChatActionListenercallback to observe the action status
PushNotificationManager.updateFcmToken(FCM_TOKEN, isSuccess, message) -> {
});

You need to call the above the method one time right after calling ChatConnectionManager.initialize() and then you have to always call in onNewToken of your firebase messaging service which extends FirebaseMessagingService.

Handling the received chat fcm message#

When you have received a firebase push notification in your firebase messaging service, you can easily identify the push notification received for the chat sdk and delegate the processing work to the sdk like below:

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String, String> notificationData = remoteMessage.getData();
if (!notificationData.isEmpty() && notificationData.containsKey("push_from") && notificationData.get("push_from").equals("MirrorFly")) {
LogMessage.d(TAG, "RemoteMessage:" + notificationData.toString());
PushNotificationManager.handleReceivedMessage(notificationData, new NotificationEventListener() {
@Override
public void onMessageReceived() {
List<Message> unseenMessagesForNotification = ChatManager.getLastNUnreadMessages(7);
for (Message message: unseenMessagesForNotification) {
String messageFrom = message.getChatUser();
if (!ChatManager.isMuted(messageFrom)) {
String senderName = ContactManager.getDisplayName(messageFrom);
//for profile pic and other info's of group/user
ProfileDetails profileDetails = ContactManager.getProfileDetails(messageFrom);
String messageType = message.getMsgType();
if (ChatType.TYPE_GROUP_CHAT.equals(message.getChatType())) {
//group chat sender user name
String groupUser = ContactManager.getDisplayName(message.getGroupChatSender());
//for profile pic and other info's of group user
ProfileDetails groupUserProfile = ContactManager.getProfileDetails(message.getGroupChatSender());
}
MessageDetail messageDetail = message.getMsgBody();
// get message details from message object and build up your notification
}
}
}
@Override
public void onGroupNotification(@NotNull String groupJid, @NotNull String titleContent, @NotNull String messageContent) {
/* Create the notification for group creation with paramter values */
}
@Override
public void onCancelNotification() {
// here you have to cancel notification
}
});
}
}

Get unread count without muted chat messages#

If you need a unread count excluding the muted chat messages, you can utilise the below method.

ChatManager.getUnreadMessageCountExceptMutedChat()

Get last N unread messages#

The below method can be used to return the last N unread messages from the sdk, so that you can utilise the data to build up your own notification

ArgumentTypeDescription
COUNTintrecent messages count to be retrieved
ChatManager.getLastNUnreadMessages(COUNT);

Group recent N unread messages#

The below method can be used to return the recent N unread messages from the sdk which are group by users/group

Map<String, List<ChatMessage>> usersWithMessage = ChatManager.getNUnreadMessagesOfEachUsers(COUNT);
ArgumentTypeDescription
COUNTintno of unread messages has to be returned for each user
info

Key of the Map holds the jid of the user/group and the value holds the list of unread message for that user/group.

info

Sdk is having a built-in functions to prepare the JID, Group JID.