Not sure if this is what you are looking for but using the package below might be the simplest way to achieve it.
flutter_local_notifications: ^5.0.0+1 // I used this version to test the concept.
Edit the LocalNotificationService configuration to your need, like to silently notify the user.
Setup the package from flutter_local_notification.
// Setting up flutter_local_notifications: ^5.0.0+1 as a Service
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class LocalNotificationService {
static final instance = LocalNotificationService();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
initialize() async {
tz.initializeTimeZones();
var initializationSettingsAndroid =
AndroidInitializationSettings('ic_notification');
var initializationSettingsIOS = IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification,
);
var initializationSettings = InitializationSettings(
iOS: initializationSettingsIOS,
android: initializationSettingsAndroid,
);
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: (String payload) {
// Do something on notification click
return null;
}
);
}
Future<void> scheduleNotification({
int id,
String body,
String title,
String payload,
DateTime scheduledNotificationDateTime,
}) async {
final epoch = scheduledNotificationDateTime.microsecondsSinceEpoch;
var androidSpecifics = AndroidNotificationDetails(
'$epoch',
title,
body,
priority: Priority.low,
importance: Importance.min,
);
var iOSSpecifics = IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = NotificationDetails(
iOS: iOSSpecifics,
android: androidSpecifics,
);
final tzTime = tz.TZDateTime.fromMicrosecondsSinceEpoch(tz.local, epoch);
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
tzTime,
platformChannelSpecifics,
payload: payload,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.wallClockTime,
);
}
Future onDidReceiveLocalNotification(
int i, String string1, String string2, String string3) async {}
Future cancelAllNotifications() async {
await flutterLocalNotificationsPlugin.cancelAll();
}
Future cancelNotification({int withId}) async {
await flutterLocalNotificationsPlugin.cancel(withId);
}
}
Call this function from anywhere,
LocalNotificationService.instance.scheduleNotification(
body: "This is test notification",
title: "Testing",
id: 1222,
scheduledNotificationDateTime: DateTime.now(),
);