showLocalNotification static method

Future<void> showLocalNotification(
  1. RemoteMessage message
)

Implementation

static Future<void> showLocalNotification(RemoteMessage message) async {
  String fullName = message.data['full_name'];
  String mobile = message.data['mobile'];
  String? messageContactId = message.data['mobile_redirect_url']?.toString();
  String? identity =
      fullName.isNotEmpty ? fullName : (mobile.isNotEmpty ? mobile : "N/A");
  String messages = message.notification?.body ?? "You have a new message.";

  if (messageContactId == activeChatContactId) {
    debugPrint(
        "FCM NOTIFICATION ---🔕 No notification: User is already chatting with $activeChatContactId");
    return;
  }

  InboxStyleInformation inboxStyle = InboxStyleInformation(
    [message.notification?.body ?? ''],
    contentTitle: identity,
    summaryText: "Tap to open chat",
  );
  var androidDetails = AndroidNotificationDetails(
    'chat_channel',
    'Chat Notifications',
    importance: Importance.high,
    playSound: true,
    priority: Priority.high,
    styleInformation: inboxStyle,
    onlyAlertOnce: true,
  );

  var notificationDetails = NotificationDetails(android: androidDetails);
  debugPrint(
      "FCM NOTIFICATION ---NOTIFICATION DATA ----------> ${jsonEncode(message.toMap())}");

  await flutterLocalNotificationsPlugin.show(
    int.parse(messageContactId ?? "0"),
    identity,
    messages,
    notificationDetails,
    payload: message.data.isNotEmpty ? jsonEncode(message.data) : null,
  );
}