displayMessageNotification static method

dynamic displayMessageNotification(
  1. int notificationId,
  2. int messageId,
  3. ProfileDetails profileDetails,
  4. String lastMessageContent,
  5. int lastMessageTime,
  6. String senderChatJID,
  7. bool autoCancel,
  8. String topicId,
)
  • Create Notification and display chat message notification
  • @parameter notificationId Unique notification id of the conversation
  • @parameter profileDetails ProfileDetails of the conversation
  • @parameter messagingStyle Unique MessagingStyle of the conversation
  • @parameter lastMessageContent Last message of the conversation
  • @parameter lastMessageTime Time of the last message

Implementation

static displayMessageNotification(
    int notificationId,
    int messageId,
    ProfileDetails profileDetails,
    String lastMessageContent,
    int lastMessageTime,
    String senderChatJID,
    bool autoCancel,
    String topicId) async {
  var title = profileDetails.getName().checkNull();
  var chatJid = profileDetails.jid.checkNull();

  debugPrint("notificationId $notificationId");
  debugPrint("notificationId $chatJid");
  // if (Platform.isIOS) {
  debugPrint("lastMessageTime $lastMessageTime");
  notificationId = (Platform.isIOS)
      ? int.parse(lastMessageTime
          .toString()
          .substring(lastMessageTime.toString().length - 5))
      : messageId;
  debugPrint("ios notification id $notificationId");
  var isGroup = profileDetails.isGroupProfile ?? false;

  var userProfile = await getProfileDetails(senderChatJID);
  var name = userProfile.name ?? '';

  title = isGroup ? "$name @ $title" : title;
  //}

  debugPrint("local notification id $notificationId");
  var channel =
      buildNotificationChannel(notificationId.toString(), null, false);

  // var notificationSounUri = SessionManagement.getNotificationUri();
  // var isVibrate = SessionManagement.getVibration();
  // var isRing = SessionManagement.getNotificationSound();

  // var unReadMessageCount = await Mirrorfly.getUnreadMessageCountExceptMutedChat();
  var unReadMessageCount = 1;
  var androidNotificationDetails = AndroidNotificationDetails(
      channel.id, channel.name,
      channelDescription: channel.description,
      importance: Importance.max,
      autoCancel: true,
      icon: AppConstants.notificationIcon,
      color: buttonBgColor,
      groupKey: groupKeyMessage,
      number: unReadMessageCount,
      groupAlertBehavior: GroupAlertBehavior.all,
      category: AndroidNotificationCategory.message,
      priority: Priority.high,
      visibility: NotificationVisibility.public,
      when: (lastMessageTime > 0) ? lastMessageTime : null,
      /*sound: isRing && notificationSounUri.checkNull().isNotEmpty ? UriAndroidNotificationSound(
          notificationSounUri!) : null,
      vibrationPattern: (isVibrate */ /*|| getDeviceVibrateMode()*/ /*) ? getDefaultVibrate() : null,*/
      vibrationPattern:
          (SessionManagement.getVibration()) ? getDefaultVibrate() : null,
      playSound: true);
  final String? notificationUri = SessionManagement.getNotificationUri();
  var iosNotificationDetail = DarwinNotificationDetails(
      categoryIdentifier: darwinNotificationCategoryPlain,
      sound: notificationUri,
      presentBadge: true,
      presentSound: true,
      presentAlert: true);

  var notificationDetails = NotificationDetails(
      android: androidNotificationDetails, iOS: iosNotificationDetail);
  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  await flutterLocalNotificationsPlugin.show(
      notificationId, title, lastMessageContent, notificationDetails,
      payload: chatJid);

  ///Cancelling the notification shown for iOS, inorder to remove from notification tray.
  ///For Push Notification from Server, will be handled at Notification service extension.
  if (autoCancel) {
    Future.delayed(const Duration(seconds: 5)).then((value) async {
      await flutterLocalNotificationsPlugin.cancel(notificationId);
    });
  }
}