showNotification method
Future<void>
showNotification(
- RemoteMessage message
)
Implementation
Future<void> showNotification(RemoteMessage message) async {
String? imageUrl = message.data['imageUrl'];
String? largeIcon = message.data['largeIcon'];
final bigPicturePath =
await DownloadFile.downloadFile(imageUrl!, 'imageNotification');
final largeIconPath =
await DownloadFile.downloadFile(largeIcon!, 'largeIconNotification');
// Create a style information object based on the image URL
BigPictureStyleInformation? styleInformation;
if (imageUrl != null || largeIcon != null) {
styleInformation = BigPictureStyleInformation(
FilePathAndroidBitmap(bigPicturePath),
largeIcon: FilePathAndroidBitmap(largeIconPath),
contentTitle: message.notification?.title.toString(),
summaryText: message.notification?.body.toString(),
);
} else {
styleInformation = null;
}
final Color notificationColor = Color(int.parse(message.data['color'].substring(1, 7), radix: 16) + 0xFF000000);
AndroidNotificationChannel channel = AndroidNotificationChannel(
Random.secure().nextInt(1000).toString(),
'High Importance Notification',
description: 'This channel is used for important notifications.',
importance: Importance.high,
);
await _flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
channel.id.toString(),
channel.name.toString(),
icon: message.data['icon'] ?? '@mipmap/ic_launcher',
channelDescription: "your channel description",
importance: Importance.high,
priority: Priority.high,
ticker: "ticker",
color: notificationColor,
styleInformation: styleInformation,
);
const DarwinNotificationDetails darwinNotificationDetails =
DarwinNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
);
NotificationDetails notificationDetails = NotificationDetails(
android: androidNotificationDetails,
iOS: darwinNotificationDetails,
);
Future.delayed(Duration.zero, () {
_flutterLocalNotificationsPlugin.show(
0,
null,
null,
notificationDetails,
payload: message.data['screen'],
);
});
}