2

Actually i want to show weekly notification in flutter, i don't find a way to i'm asking here :-)

So in my app user can schedule timetable and got notification according to that. And user can schedule notifcation for all 7 days of week(monday, tuesday ... sunday), now i want to repeat these notification on weekly basis, so if user scheduled notification for monday so that notification will repeat on monday, and if it is scheduled for tuesday it will repeat on tuesday and so on.

 static Future showWeeklyNotification({
    required int id,
    String? title,
    String? body,
    String? payload,
    required int weekday,
    required Time scheduleTime,
  }) async => _notifications.zonedSchedule(
    id,
    title,
    body,
    _scheduleWeekly(scheduleTime , weekday, ),
    await _notificationDetails(),
    androidAllowWhileIdle: true,
    uiLocalNotificationDateInterpretation:
    UILocalNotificationDateInterpretation.absoluteTime,
    matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime,
  );


  static tz.TZDateTime _scheduleDaily(Time time){
    final now = tz.TZDateTime.now(tz.local);
    final scheduleDate = tz.TZDateTime(tz.local, now.year, now.month, now.day, time.hour, time.minute, time.second);

    return scheduleDate.isBefore(now) ? scheduleDate.add(Duration(days: 1)) : scheduleDate;
  }


  static tz.TZDateTime _scheduleWeekly(Time time, weekDay){
    tz.TZDateTime scheduleDate = _scheduleDaily(time);
    print("hour");
    print(time.hour);
    print(time.minute);
    print("weekday");
    print(weekDay);
    while(scheduleDate.weekday != weekDay){
      scheduleDate = scheduleDate.add(const Duration(days: 1));
    }

    return scheduleDate;
  }

I am using this code but it's not working, can anyone help me to find out what's wrong.

If i schedule it for today then it will work, but if i schedule it for next day or previous day it seems to not work.

Thankyou in advance.

Sushant
  • 21
  • 1

0 Answers0