showRichCard method

Future<void> showRichCard(
  1. String msg,
  2. String msgid
)

Implementation

Future<void> showRichCard(String msg, String msgid) async {
  // _createNotificationChannel();
  // Logic to show rich card notification

  String jsonString = msg;

  // Clean JSON string
  if (jsonString.startsWith('"') && jsonString.endsWith('"')) {
    jsonString = jsonString.substring(1, jsonString.length - 1);
  }

  // Decode JSON string
  jsonString = jsonString
      .replaceAll(r'\"', '"')
      .replaceAll(r'\\', '\\')
      .replaceAll(r'\/', '/')
      .replaceAll(r'\b', '\b')
      .replaceAll(r'\f', '\f')
      .replaceAll(r'\n', '\n')
      .replaceAll(r'\r', '\r')
      .replaceAll(r'\t', '\t');

  late String richTitle;
  late String richDesc;
  late String richImage;
  late String richUrl;

  try {
    final Map<String, dynamic> jsonObject = json.decode(jsonString);

    // Access JSON fields
    richTitle = jsonObject['title'] ?? '';
    richDesc = jsonObject['discretion'] ?? '';
    richImage = jsonObject['image'] ?? '';
    richUrl = jsonObject['url'] ?? '';

    if (richUrl.isEmpty || !Uri.parse(richUrl).isAbsolute) {
      richUrl = "https://www.leewaysoftech.com/";
    }
  } catch (e) {
    print("Error parsing JSON: $e");
    return; // Exit if JSON parsing fails
  }

  // Download image from URL
  final Uint8List? bitmap = await _downloadRichCardImage(richImage);

  // Create notification channel
  const String channelId = "rich_card_channel";
  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    channelId,
    'Rich Card Notifications',
    description: 'Channel for rich card notifications',
    importance: Importance.high,
    playSound: true,
  );

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  // Create a unique request code using hash code of msgId
  int requestCode = msgid.hashCode;

  // Create custom notification layout
  final NotificationDetails platformChannelSpecifics = NotificationDetails(
    android: AndroidNotificationDetails(
      channel.id,
      channel.name,
      channelDescription: channel.description,
      importance: Importance.high,
      priority: Priority.high,
      styleInformation: BigPictureStyleInformation(
        ByteArrayAndroidBitmap(bitmap!),
        contentTitle: richTitle,
        summaryText: richDesc,
        hideExpandedLargeIcon: true,
      ),
    ),
  );

  // Create an intent for the URL action
  final Uri uri = Uri.parse(richUrl);

  await flutterLocalNotificationsPlugin.show(
    requestCode,
    richTitle,
    richDesc,
    platformChannelSpecifics,
    payload: richUrl, // Pass the URL as payload
  );

  // Define Initialization Settings
  final InitializationSettings initializationSettings =
      InitializationSettings(
    android:
        AndroidInitializationSettings('@mipmap/ic_launcher'), // Your app icon
  );

  // Handle notification tap to open URL
  await flutterLocalNotificationsPlugin.initialize(
    initializationSettings,
    onDidReceiveNotificationResponse: (NotificationResponse response) async {
      final String? payload = response.payload;
      if (payload != null) {
        await launchUrl(Uri.parse(payload));
        await clickCount(msgid, "RichCard_ButtonClick",
            richUrl); // Insert click data into database
      }
    },
  );
}