首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >特定日期范围内每天的通知

特定日期范围内每天的通知
EN

Stack Overflow用户
提问于 2020-09-16 17:28:29
回答 1查看 173关注 0票数 1

因此,我有一个应用程序,它提醒用户在特定的日期间隔内每天在特定的时间服用药物。例如,用户可以选择从2020年9月16日到2020年9月18日的某一天的某个时间接收通知

我的方法是:我使用带有showDailyAtTime()函数的flutter_local_notifications包来调度通知。然而,我面临的问题是,假设我不再打开应用程序,没有办法取消预定的通知,因此,即使在指定的日期范围之后,通知也会弹出。我希望通知是离线的,所以Firebase似乎不是一个选择。

EN

回答 1

Stack Overflow用户

发布于 2020-09-16 17:53:34

你可以用FlutterLocalNotificationsPlugin来解决这个问题。

方法是在每次启动应用程序时调用rescheduleNotifications方法。在该方法中,删除了所有通知,并设置了下一个通知。例如,在calculateNotificationTimes中,您计算未来30天的所有通知。例如,所有在2020年9月16日至2020年9月18日的通知都是在您选择的时间内发出的。

代码语言:javascript
复制
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

Future<void> rescheduleNotifications() async {
  final localNotificationsPlugin = FlutterLocalNotificationsPlugin();
  const initializationSettings = InitializationSettings(AndroidInitializationSettings('app_icon'), IOSInitializationSettings());
  const androidChannelSpecifics = AndroidNotificationDetails('your channel id', 'your channel name', 'your channel description');
  const iOSNotificationDetails = IOSNotificationDetails();
  const notificationsDetails = NotificationDetails(androidChannelSpecifics, iOSNotificationDetails);

  await localNotificationsPlugin.initialize(initializationSettings);
  await localNotificationsPlugin.cancelAll();

  // Calculate the next notifications.
  final notificationTimes = calculateNotificationTimes();

  var _currentNotificationId = 0;
  for (final time in notificationTimes) {
    localNotificationsPlugin.schedule(
      _currentNotificationId++,
      "It's time to take your medicine.",
      'Take the red pill',
      time,
      notificationsDetails,
      androidAllowWhileIdle: true,
    );
  }
}

在iOS上,您只能启用64个通知。这种方法在iOS上的缺点是,如果用户在64次通知后没有打开应用程序,则不会显示任何通知。我认为这很好,因为用户似乎不再使用这个应用程序了。

未测试代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63917079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档