1

实际上我想在颤动中显示每周通知,我找不到我在这里问的方法:-)

因此,在我的应用程序中,用户可以安排时间表并据此获得通知。并且用户可以为一周中的所有 7 天(星期一、星期二 ...定于星期二,它将在星期二重复,依此类推。

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

我正在使用此代码,但它不起作用,谁能帮我找出问题所在。

如果我把它安排在今天,那么它会起作用,但如果我把它安排在第二天或前一天,它似乎不起作用。

先感谢您。

4

0 回答 0