有一种方法可以使用本地通知每隔4小时发送一次通知吗?

huangapple go评论69阅读模式
英文:

There is a way to send a notification every 4 hours with local notifications?

问题

我想知道是否有办法使用`flutter_local_notifications`每隔4小时发送一次通知。

这个包有一个重复通知的功能,但你只能在以下4个选项中选择:

`RepeatInterval.everyMinute`
`RepeatInterval.hourly`
`RepeatInterval.daily`
`RepeatInterval.weekly`

代码片段:

```Dart
未来<void> _repeatNotification() async {
    const AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails(
            'repeating channel id', 'repeating channel name',
            channelDescription: 'repeating description');
    const NotificationDetails notificationDetails =
        NotificationDetails(android: androidNotificationDetails);
    await flutterLocalNotificationsPlugin.periodicallyShow(
      id++,
      'repeating title',
      'repeating body',
      RepeatInterval.hourly, // <-- 在这里
      notificationDetails,
      androidAllowWhileIdle: true,
    );
  }

有人知道是否有办法以特定的小时数重复,而不是一般的“每小时”吗?谢谢


<details>
<summary>英文:</summary>

I&#39;m wondering if there is a way to send a notification every 4 hours with `flutter_local_notifications`.

This package have a repeat notification utility but you have to choose between a repeat interval, but it only provides 4 choices:

`RepeatInterval.everyMinute`
`RepeatInterval.hourly`
`RepeatInterval.daily`
`RepeatInterval.weekly`

A code snippet:

```Dart
Future&lt;void&gt; _repeatNotification() async {
    const AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails(
            &#39;repeating channel id&#39;, &#39;repeating channel name&#39;,
            channelDescription: &#39;repeating description&#39;);
    const NotificationDetails notificationDetails =
        NotificationDetails(android: androidNotificationDetails);
    await flutterLocalNotificationsPlugin.periodicallyShow(
      id++,
      &#39;repeating title&#39;,
      &#39;repeating body&#39;,
      RepeatInterval.hourly, // &lt;-- HERE
      notificationDetails,
      androidAllowWhileIdle: true,
    );
  }

Someone know if there is a way to repeat with a precise amount of hours instead a generic "hourly"? Thanks

答案1

得分: 1

以下是要翻译的内容:

您可以使用以下任一软件包:

  1. https://pub.dev/packages/workmanager
  2. https://pub.dev/packages/background_fetch

这些软件包允许您在应用程序中安排后台任务。

然后,您可以使用 "flutter_local_notifications" 软件包作为任务的一部分触发通知。

英文:

You can use either of these packages:

  1. https://pub.dev/packages/workmanager
  2. https://pub.dev/packages/background_fetch

These packages allow you to schedule background tasks in your app.

Then you can use flutter_local_notifications package to trigger a notification as part of the task.

huangapple
  • 本文由 发表于 2023年4月17日 00:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定