-0.4 C
New York
Saturday, February 22, 2025

ios – Methods to skip one recurring notification with flutter native notifications?


I am utilizing Flutter Native Notifications to ship a reminder to customers each day at a time of their alternative. My schedule perform in my notification service seems to be like this:

  Future scheduleDailyNotification(
      DateTime setTime, String title, String physique, String payload) async {
    // Setup notification particulars
    const DarwinNotificationDetails iosNotificationDetails =
        DarwinNotificationDetails(presentAlert: true, presentSound: true);
    const NotificationDetails notificationDetails =
        NotificationDetails(iOS: iosNotificationDetails);

    // Convert to TZDateTime
    closing tz.TZDateTime scheduledDate = tz.TZDateTime(
      tz.native,
      setTime.yr,
      setTime.month,
      setTime.day,
      setTime.hour,
      setTime.minute,
      setTime.second,
    );

    logger.d('Scheduling notification for: $scheduledDate');
    // Schedule notification
    await _flutterLocalNotificationsPlugin.zonedSchedule(
        0, title, physique, scheduledDate, notificationDetails,
        payload: payload,
        uiLocalNotificationDateInterpretation:
            UILocalNotificationDateInterpretation.absoluteTime,
        matchDateTimeComponents: DateTimeComponents.time);
  }

Now this reminder ought to solely be despatched if the consumer didn’t take a particular motion within the app. If the consumer takes this motion, I wish to cancel todays notification, however begin sending out notifications once more tomorrow:

  Future cancelTodaysNotification() async {
    closing tz.TZDateTime now = tz.TZDateTime.now(tz.native);
    DateTime setTime = await _getNotificationsTimeFromPrefs();
    closing tz.TZDateTime scheduledDate = tz.TZDateTime(
        tz.native, now.yr, now.month, now.day, setTime.hour, setTime.minute);

    if (now.isBefore(scheduledDate)) {
      logger.d('Cancelling notification for right now');
      await _flutterLocalNotificationsPlugin.cancel(0);

      // Get subsequent legitimate date and add someday
      closing tomorrow =
          _nextInstanceOfTime(setTime).add(const Length(days: 1));
      await scheduleDailyNotification(tomorrow, _notificationTitle,
          _notificationBody, _notificationPayload);
    }
  }

This doesn’t work as a result of matchDateTimeComponents: DateTimeComponents.time implies that the date is ignored. It’ll simply schedule a notification for right now once more (provided that the time is sooner or later)

Is there a method to cancel right now’s notification with out including a background course of to the app?

One thought I had is to make use of DateTimeComponents.dayOfWeekAndTime as a substitute of DateTimeComponents.time. If right now is Wednesday I might cancel all notifications after which schedule them once more for each day besides Wednesday. BUT this may imply that to any extent further no notifications shall be despatched on Wednesdays…

This looks as if a really apparent function and I hope I am lacking one thing.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles