Flutter -令人敬畏的警告-尝试执行重复的计划通知

vatpfxk5  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(104)

我正在做一个水提醒,需要在上午8点,下午12点,下午4点和晚上8点发送通知。
这是我的代码,当我单击通知按钮时,它工作正常,我唯一的问题是重复,我不知道如何做我想做的事情,即使在做研究之后。

await AwesomeNotifications().createNotification(
      content: NotificationContent(
        id: id,
        channelKey: 'high_importance_channel',
        title: title,
        body: body,
        actionType: actionType,
        notificationLayout: notificationLayout,
        summary: summary,
        category: category,
        payload: payload,
        bigPicture: bigPicture,
      ),
      actionButtons: actionButtons,
      schedule: scheduled ? NotificationInterval(
        interval: interval,
        timeZone: await AwesomeNotifications().getLocalTimeZoneIdentifier(),
        preciseAlarm: true,
      )
          : null,
    );

字符串

vuv7lop3

vuv7lop31#

我找到了一个解决这个问题的方法,即在所需的时间调用不同的通知方法

Future <void> showScheduledNotification8AM({
    int id = 8,
    String? title,
    String? body,
    String? summary,
    Map <String, String>? payload,
    ActionType actionType = ActionType.Default,
    NotificationLayout notificationLayout = NotificationLayout.Default,
    NotificationCategory? category,
    String? bigPicture,
    List <NotificationActionButton>? actionButtons,
    bool? scheduled,
    int? interval,
    int? scheduledTime = 8,
  }) async {
    await AwesomeNotifications().createNotification(
      content: NotificationContent(
        id: id,
        channelKey: 'scheduled',
        title: title,
        body: body,
        actionType: actionType,
        notificationLayout: notificationLayout,
        summary: summary,
        category: category,
        payload: payload,
        bigPicture: bigPicture,
      ),
      actionButtons: actionButtons,
      schedule: NotificationCalendar(hour: scheduledTime, minute: 0, repeats: false,)
    );
  }
  Future <void> showScheduledNotification12PM({
    int id = 12,
    String? title,
    String? body,
    String? summary,
    Map <String, String>? payload,
    ActionType actionType = ActionType.Default,
    NotificationLayout notificationLayout = NotificationLayout.Default,
    NotificationCategory? category,
    String? bigPicture,
    List <NotificationActionButton>? actionButtons,
    bool? scheduled,
    int? interval,
    int? scheduledTime = 12,
  }) async {
    await AwesomeNotifications().createNotification(
        content: NotificationContent(
          id: id,
          channelKey: 'scheduled',
          title: title,
          body: body,
          actionType: actionType,
          notificationLayout: notificationLayout,
          summary: summary,
          category: category,
          payload: payload,
          bigPicture: bigPicture,
        ),
        actionButtons: actionButtons,
        schedule: NotificationCalendar(hour: scheduledTime, minute: 0, repeats: false,)
    );
  }
  Future <void> showScheduledNotification4PM({
    int id = 16,
    String? title,
    String? body,
    String? summary,
    Map <String, String>? payload,
    ActionType actionType = ActionType.Default,
    NotificationLayout notificationLayout = NotificationLayout.Default,
    NotificationCategory? category,
    String? bigPicture,
    List <NotificationActionButton>? actionButtons,
    bool? scheduled,
    int? interval,
    int? scheduledTime = 16,
  }) async {
    await AwesomeNotifications().createNotification(
        content: NotificationContent(
          id: id,
          channelKey: 'scheduled',
          title: title,
          body: body,
          actionType: actionType,
          notificationLayout: notificationLayout,
          summary: summary,
          category: category,
          payload: payload,
          bigPicture: bigPicture,
        ),
        actionButtons: actionButtons,
        schedule: NotificationCalendar(hour: scheduledTime, minute: 32, repeats: false,)
    );
  }
  Future <void> showScheduledNotification8PM({
    int id = 20,
    String? title,
    String? body,
    String? summary,
    Map <String, String>? payload,
    ActionType actionType = ActionType.Default,
    NotificationLayout notificationLayout = NotificationLayout.Default,
    NotificationCategory? category,
    String? bigPicture,
    List <NotificationActionButton>? actionButtons,
    bool? scheduled,
    int? interval,
    int? scheduledTime = 20,
  }) async {
    await AwesomeNotifications().createNotification(
        content: NotificationContent(
          id: id,
          channelKey: 'scheduled',
          title: title,
          body: body,
          actionType: actionType,
          notificationLayout: notificationLayout,
          summary: summary,
          category: category,
          payload: payload,
          bigPicture: bigPicture,
        ),
        actionButtons: actionButtons,
        schedule: NotificationCalendar(hour: scheduledTime, minute: 0, repeats: false,)
    );
  }

字符串
并使用onChange函数调用各个方法。

相关问题