showLocalNotification method

Future<void> showLocalNotification(
  1. ChatMessageModel chatMessageModel
)

Implementation

Future<void> showLocalNotification(ChatMessageModel chatMessageModel) async {
  debugPrint("showing local notification");
  var isUserMuted =
      await Mirrorfly.isChatMuted(jid: chatMessageModel.chatUserJid);
  var isUserUnArchived =
      await Mirrorfly.isChatUnArchived(jid: chatMessageModel.chatUserJid);
  var isArchivedSettingsEnabled = await Mirrorfly.isArchivedSettingsEnabled();

  var archiveSettings = isArchivedSettingsEnabled.checkNull()
      ? isUserUnArchived.checkNull()
      : true;

  if (!chatMessageModel.isMessageSentByMe &&
      !isUserMuted.checkNull() &&
      archiveSettings) {
    final String? notificationUri = SessionManagement.getNotificationUri();
    final UriAndroidNotificationSound uriSound =
        UriAndroidNotificationSound(notificationUri!);
    debugPrint("notificationUri--> $notificationUri");

    var messageId = chatMessageModel.messageSentTime
        .toString()
        .substring(chatMessageModel.messageSentTime.toString().length - 5);
    debugPrint("Mani Message ID $messageId");
    AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails(chatMessageModel.messageId, 'MirrorFly',
            importance: Importance.max,
            priority: Priority.high,
            sound: uriSound,
            styleInformation: const DefaultStyleInformation(true, true));
    DarwinNotificationDetails iosNotificationDetails =
        DarwinNotificationDetails(
            categoryIdentifier: darwinNotificationCategoryPlain,
            sound: notificationUri,
            presentSound: true,
            presentBadge: true,
            presentAlert: true);

    NotificationDetails notificationDetails = NotificationDetails(
        android: androidNotificationDetails, iOS: iosNotificationDetails);
    await flutterLocalNotificationsPlugin.show(
        12345,
        chatMessageModel.senderUserName,
        chatMessageModel.isMessageRecalled.value
            ? "This message was deleted"
            : chatMessageModel.messageTextContent,
        notificationDetails,
        payload: chatMessageModel.chatUserJid);
  } else {
    debugPrint("self sent message don't need notification");
  }
}