showPushNotification static method

Future<void> showPushNotification(
  1. Map notification
)

Implementation

static Future<void> showPushNotification(Map notification) async {
  if (!isInitializedPlugin) {
    await initializePlugin();
  }

  DarwinNotificationDetails iosDetails = const DarwinNotificationDetails();
  AndroidNotificationDetails androidDetails =
      const AndroidNotificationDetails('default', 'channelName');

  String title = 'Backendless Title';
  String message = 'Backendless Message';
  int badge = 0;
  String? threadId;
  String? subtitle;
  int id = Random().nextInt(2147483646);
  String? templateName = notification['template_name'];

  if (notification.containsKey('data')) {
    templateName ??= notification['data']['template_name'];
  }

  if (templateName == null) {
    if (Platform.isAndroid) {
      title = notification['android-content-title'];
    }
    if (Platform.isIOS) {
      title = notification['ios-alert-title'];
    }

    message = notification['message'];
  } else {
    Map? templateFromStorage;
    if (await TemplateStorage.containsTemplate(templateName)) {
      String templateAsString =
          await TemplateStorage.getTemplate(templateName) as String;
      templateFromStorage = jsonDecode(templateAsString);
    }

    if (Platform.isIOS) {
      badge = notification['aps']['badge'] ?? 0;
      threadId = notification['thread-id'];
      // String? summaryFormat = notification['summary-format'];

      message = notification['message'];
      title = notification['ios-alert-title'];
      subtitle = notification['aps']['alert']['subtitle'];
      iosDetails = DarwinNotificationDetails(
          badgeNumber: badge, subtitle: subtitle, threadIdentifier: threadId);
    } else if (Platform.isAndroid) {
      if (templateFromStorage != null) {
        badge = templateFromStorage['badgeNumber'] ?? 0;
        Color? color = Color(templateFromStorage['colorCode']);

        if (notification.containsKey('data')) {
          message = notification['data']['message'];
          title = notification['data']['android-content-title'];
        } else {
          message = notification['message'];
          title = notification['android-content-title'];
          subtitle = notification['android-summary-subtext'];
        }

        androidDetails = AndroidNotificationDetails(
          templateName,
          templateName,
          priority: Priority(templateFromStorage['priority'] - 3),
          importance: Importance(templateFromStorage['priority']),
          subText: subtitle,
          number: badge == 0 ? null : badge,
          channelShowBadge: badge == 0 ? false : true,
          icon: templateFromStorage['icon'],
          color: color,
        );
      }
    }
  }

  notification['flutter_notification_identifier'] = 'identity';
  String payload = jsonEncode(notification);

  var notificationDetails =
      NotificationDetails(iOS: iosDetails, android: androidDetails);

  await flutterLocalNotificationsPlugin
      .show(id, title, message, notificationDetails, payload: payload);
}