【问题标题】:Notifications every day for a particular date range特定日期范围内的每日通知
【发布时间】:2020-09-16 09:28:29
【问题描述】:

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

我的方法:我使用带有showDailyAtTime() 函数的flutter_local_notifications 包安排通知。但是,我面临的问题是,假设我不再打开应用程序,则无法取消预定的通知,因此即使在指定的日期范围之后也会弹出通知。我希望通知处于离线状态,因此 Firebase 似乎不是一个选项。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    你可以用FlutterLocalNotificationsPlugin解决问题。

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

    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 个通知后没有打开应用,则不会显示任何通知。我认为这很好,因为用户似乎不再使用该应用程序了。

    没有测试代码。

    【讨论】:

    • 这样,除非我打开应用程序,否则通知不会每天出现,对吧?此外,即使是取消也只有在我打开应用程序时才会发生。我认为这会带来同样的问题
    • 使用此方法可以每天显示通知。为此,“calculateNotificationTimes”方法只需每天输出一次。与 showDailyAtTime 相比,它的优势在于不会显示不必要的通知。
    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2020-10-09
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多