Local Notifications 2023。如何设置定期通知?

问题描述 投票:0回答:1

我在一个应用程序中工作,我需要根据用户输入实现本地通知。这意味着用户将选择应用何时显示通知。我找到了方法repeatInterval,但它只提供固定的时间间隔,例如

每分钟 → const RepeatInterval

每小时 → const RepeatInterval

每天 → const RepeatInterval

每周 → const RepeatInterval

我不需要这些选项,因为它们是固定的。 我需要添加一个通知,以及运行时的间隔。

我试过,

NotificationDetails notificationDetails =
 NotificationDetails(android: androidNotificationDetails);
await _flutterLocalNotificationsPlugin.zonedSchedule(
  -1,
  title,
  body,
  tz.TZDateTime.from(
    DateTime.now().add(const Duration(hours:  hours)), 
    tz.local
  ),
  androidAllowWhileIdle: true,
  notificationDetails,
  uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime
);

通过这种方法,我可以手动传递第一个通知,但我需要重复通知 X 次,直到用户取消通知。

有什么想法吗?

flutter duration flutter-local-notification
1个回答
0
投票

flutter_local_notifications
插件本身不提供此功能。您有以下选择。

  • 您可以创建自己的插件分支并创建
    periodically_show
    函数的变体。 periodically_showandroid实现
    iOS实现
    内部支持任意时间间隔。只是插件的API设计选择了隐藏这个功能。
  • 您可以将
    flutter_local_notifications
    与后台工作程序库
    WorkerManager
    结合使用,使您的应用程序能够在后台执行某些功能。在每次通话中,您都可以安排通知。
  • 您可以提前X次计算调度时间并批量安排您的通知。然后,当用户更改他们的通知首选项时,您可以再次取消它们。

我认为这样的功能会对

flutter_local_notifications
库做出很大的贡献。

© www.soinside.com 2019 - 2024. All rights reserved.