In my flutter I have implemented flutter_local_notifications
. I am using scheduled notification and it is working fine but what I want is when Application is running it should show scheduled notification in alert box instead of sending notification and when app not running then send actual notification. Note: I am using flutterLocalNotificationsPlugin.zonedSchedule
to schedule the notification and it's working fine.
I Just want to show scheduled notification in alert box when application is running.
here is my code
Future<void> _scheduleNotification(DateTime initialTime, String title, String body) async {
final tz.TZDateTime scheduledDate = tz.TZDateTime.from(initialTime.subtract(const Duration(minutes: 5)), tz.local);
final notificationId = initialTime.millisecondsSinceEpoch~/100000;
final currentTime = tz.TZDateTime.now(tz.local);// Check if scheduledDate is in the past
if (scheduledDate.isBefore(currentTime)) {
return;
}
final prefs = await SharedPreferences.getInstance();
final notificationEnabled = prefs.getBool('notificationEnabled') ?? true;
if (!notificationEnabled) {
return;
}
AndroidNotificationDetails androidPlatformChannelSpecifics = const AndroidNotificationDetails(
'channel_id', 'channel_name',
importance: Importance.max,
priority: Priority.high,
showWhen: true,
);
const iosNotificationDetail = DarwinNotificationDetails(
presentSound: true,
presentBadge: true,
);
NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics,iOS: iosNotificationDetail);
await flutterLocalNotificationsPlugin.zonedSchedule(
notificationId, // Use the hashed value as the Notification ID
title, // Notification Title
body, // Notification Body
scheduledDate,
platformChannelSpecifics,
androidAllowWhileIdle: true,
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
);
}