showRichCard method
Implementation
Future<void> showRichCard(String msg, String msgid) async {
createNotificationChannel();
String jsonString = msg;
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);
richTitle = jsonObject['title'] ?? '';
richDesc = jsonObject['discretion'] ?? '';
richImage = jsonObject['image'] ?? '';
richUrl = jsonObject['url'] ?? '';
if (richUrl == null ||richUrl.isEmpty || !Uri.parse(richUrl).isAbsolute) {
richUrl = "https://www.leewaysoftech.com/";
}
} catch (e) {
debugPrint("Error parsing JSON: $e");
return; // Exit if JSON parsing fails
}
// Download image for iOS
String? localImagePath;
if (Platform.isIOS) {
localImagePath =
await _downloadAndSaveImage(richImage, 'notification_richcard.jpg');
}
// 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,
);
// iOS-specific notification details with image attachment
final DarwinNotificationDetails iOSPlatformChannelSpecifics =
DarwinNotificationDetails(
attachments: localImagePath != null
? [DarwinNotificationAttachment(localImagePath)]
: [],
);
// 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,
icon: '@mipmap/notify_adaptive_fore',
// Ensure this matches the icon name exactly
styleInformation: BigPictureStyleInformation(
ByteArrayAndroidBitmap(bitmap!),
contentTitle: richTitle,
summaryText: richDesc,
hideExpandedLargeIcon: true,
),
),
iOS: iOSPlatformChannelSpecifics,
);
// Store msgid, redirectUrl, and type in the payload for later retrieval
String payload = jsonEncode({
'url': richUrl,
'msgid': msgid,
'type': 'Rich Card', // Include the type here
});
await flutterLocalNotificationsPlugin.show(
requestCode,
richTitle,
richDesc,
platformChannelSpecifics,
payload: payload, // Pass the URL as payload
);
}