handleForegroundNotification static method

Future<void> handleForegroundNotification({
  1. required RemoteMessage remoteMessage,
  2. Color? backgroundColor,
})

Implementation

static Future<void> handleForegroundNotification({
  required RemoteMessage remoteMessage,
  Color? backgroundColor,
}) async {
  try {
    final Map<String, dynamic> data = remoteMessage.data;
    final rawData = data['additional_data'];

    if (rawData != null) {
      try {
        final parsed = json.decode(rawData);
        final inapp = parsed['inapp_message'] == true;

        if (inapp) {
          const storage = FlutterSecureStorage();
          await storage.write(key: "inapp", value: rawData);
          final model = InAppModel.fromJson(parsed);
          InngageDialog.showInAppDialog(model);
        }
      } catch (e) {
        debugPrint('Failed to parse additional_data or handle inapp: $e');
      }
    }

    final notification = remoteMessage.notification;

    if (notification != null) {
      final String title = notification.title ?? '';
      final String body = notification.body ?? '';

      NotificationDetails details;

      if (Platform.isAndroid) {
        final androidDetails = AndroidNotificationDetails(
          'high_importance_channel',
          'your channel name',
          channelDescription: 'your channel description',
          importance: Importance.max,
          priority: Priority.high,
          color: backgroundColor ?? Colors.blue,
        );
        details = NotificationDetails(android: androidDetails);
      } else if (Platform.isIOS) {
        const iOSDetails = DarwinNotificationDetails(
          presentAlert: true,
          presentBadge: true,
          presentSound: true,
        );
        details = const NotificationDetails(iOS: iOSDetails);
      } else {
        details = const NotificationDetails();
      }

      if (title.isNotEmpty && body.isNotEmpty) {
        await flutterLocalNotificationsPlugin.show(
          0,
          title,
          body,
          details,
          payload: json.encode(data),
        );
      }
    }
  } catch (e) {
    debugPrint('handleForegroundNotification error: $e');
  }
}