0

如何使用 flutter_local_notification 包触发特定时间段的通知。我尝试了 pub.dev 上的教程,但我仍然无法做到。有人,请帮忙。

这是我在下午 4:23 触发它的尝试。

  tz.TZDateTime _nextInstanceOfTenAM() {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, 16, 23));
if (scheduledDate.isBefore(now)) {
  scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;



Future<void> displayNotification() async {
notificationsPlugin.zonedSchedule(
    0,
    "Wake Up",
    'Time to wake up now',
    _nextInstanceOfTenAM(),
    //tz.TZDateTime.now(tz.local).add(Duration(seconds: 3)),
    NotificationDetails(
      android: AndroidNotificationDetails(
          'channel id', 'channel name', 'channel description'),
    ),
    uiLocalNotificationDateInterpretation:
    UILocalNotificationDateInterpretation.absoluteTime,
    androidAllowWhileIdle: true);

}

4

1 回答 1

0

你必须在flutter中注册一个后台服务。请通过以下链接-

https://flutter.dev/docs/resources/faq#can-i-run-dart-code-in-the-background-of-an-flutter-app

如何创建在应用程序关闭时也能正常工作的 Flutter 后台服务

于 2021-06-15T19:04:29.273 回答