Push notification

Prerequisites#

Your android app should have a firebase cloud messaging integration to receive push notifications. Refer this doc to continue firebase cloud messaging integration

Add runtime permissions for push notification#

From Android 13, To receive notification message, we need below permissions:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

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")) {
PushNotificationManager.handleReceivedMessage(notificationData, new NotificationEventListener() {
@Override
public void onMessageReceived(@NonNull ChatMessage chatMessage) {
// get unseen message list
List<ChatMessage> unseenMessagesForNotification = FlyMessenger.getLastNUnreadMessages(7);
for (ChatMessage message : unseenMessagesForNotification) {
String chatUserJid = message.getChatUserJid();
if (!ChatManager.isMuted(chatUserJid)) {
//for profile pic and other info's of group/user
ProfileDetails profileDetails = ContactManager.getProfileDetails(chatUserJid);
if (ChatTypeEnum.groupchat.equals(message.getMessageChatType())) {
//group chat sender user name
String groupUserName = ContactManager.getDisplayName(message.getSenderUserJid());
//for profile pic and other info's of group user
ProfileDetails groupUserProfile = ContactManager.getProfileDetails(message.getSenderUserJid());
}
//build up your notification
}
}
}
@Override
public void onGroupNotification(@NotNull String groupJid, @NotNull String titleContent, @NonNull ChatMessage chatMessage) {
/* Create the notification for group creation with parameter 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.

FlyMessenger.getUnreadMessageCountExceptMutedChat()

Get unread count without muted private chat messages#

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

FlyMessenger.getUnreadPrivateChatUserMessageCount()

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.