我想在特定时间执行一个功能,但是当应用程序关闭时。该功能将显示通知。
这是我要执行的功能:
Future<void> dailyNotification() async {
DateTime now= DateTime.now();
Time reviewTime = Time(13, 02, 20 );
DateTime notTime = DateTime(now.year, now.month, now.day, reviewTime.hour, reviewTime.minute, reviewTime.second);
var androidChannelSpecifics = AndroidNotificationDetails(
'CHANNEL_ID 2',
'CHANNEL_NAME 2',
"CHANNEL_DESCRIPTION 2",
importance: Importance.max,
priority: Priority.high,
);
var iosChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics =
NotificationDetails(android: androidChannelSpecifics, iOS: iosChannelSpecifics);
await flutterLocalNotificationsPlugin.zonedSchedule(
2,
'Test Title at ${reviewTime.hour}:${reviewTime.minute}.${reviewTime.second}',
'Test Body', //null
notTime,
platformChannelSpecifics,
payload: 'Test Payload',
);
}
编辑:
根据 Tirth Patel 在帖子中的建议,我已经这样实现了:
void callbackDispatcher() {
Workmanager().executeTask((taskName, inputData) async {
notificationPlugin.showDailyAtTime();
return Future.value(true);
});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Workmanager().initialize(callbackDispatcher);
Workmanager().registerPeriodicTask(
"1",
"simplePeriodicTask",
frequency: Duration(minutes: 15),
);
runApp(MyApp());
}
但是,它仍然不起作用。我正在使用颤振本地通知包。
我还不能解决这个问题。
请帮我解决一下这个。
感谢您的回复。