Flutter - Awesome Notifications - trying to perform repeat scheduled notifications

313 views Asked by At

I am making a water reminder and need a notification to be sent at 8 am, 12 pm, 4 pm and 8 pm.

Here is my code, when I single click the notification button, it works fine, my only issue is to get to repeat, i have no clue how do go about what i want to do even after doing research.

    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,
    );
1

There are 1 answers

0
Retired On BEST ANSWER

I found a solution to this problem i encountered by making different notification methods each calling at the desired times

  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,)
    );
  }

and used onChange function to call each method.