1

I want to set a notification at a time to receive and send notification time so that I cannot receive notification any longer. But on the next day, I want to receive a notification as I set the duration yesterday.

  print("Background Task---------- $time");
if (startTimeStr != null &&
    endTimeStr != null &&
    notificationDuration != null) {
  print(
      "Date : ${startTimeStr} , end: ${endTimeStr}, Duration: $notificationDuration  ");
  final startTime = DateTime.parse(startTimeStr).toUtc();
  final endTime = DateTime.parse(endTimeStr).toUtc();
  if (startTime.isBefore(time)) {
    final convertedStartTime = DateTime(time.year, time.month, time.day,
            startTime.hour, startTime.minute)
        .toUtc();
    final convertedEndTime = DateTime(
            time.year, time.month, time.day, endTime.hour, endTime.minute)
        .toUtc();
    print("Notificating Creating");
    final duration = convertedEndTime.difference(convertedStartTime);
    final scheduleTimes = <DateTime>[];
    for (int i = 0; i < duration.inMinutes; i++) {
      ///if (notificationDuration < duration.inMinutes) {
      final mintuesDuration =
          Duration(minutes: int.parse(notificationDuration) * i);
      final mint = convertedStartTime.add(mintuesDuration);

      scheduleTimes.add(mint.toUtc());
      // }
    }
    print(
        "ConvertedTime: $convertedStartTime and EndTime: $convertedEndTime and Duration $duration and Current Time ${time}");
    AwesomeNotifications()
        .createNotification(
      content: NotificationContent(
        channelKey: 'test_channel',
        title: 'T: $convertedStartTime',
        id: 10,
        body: 'Body: $convertedEndTime and Duration $duration\n $time',
        wakeUpScreen: true,
        criticalAlert: true,
      ),
      schedule: NotificationAndroidCrontab(
          repeats: true,
          preciseAlarm: true,
          allowWhileIdle: true,
          initialDateTime: DateTime.now(),
          expirationDateTime: DateTime.now().add(Duration(minutes: 10)),
          timeZone: AwesomeNotifications.utcTimeZoneIdentifier,
          preciseSchedules: List.generate(
              10,
              (index) =>
                  convertedStartTime.add(Duration(minutes: 1)).toUtc())),
    )
        .then((value) {
      print(
          "==============Notification NotificationAndroidCrontab==============");
    }).catchError((error) {
      print(
          "==============Notification Error : NotificationAndroidCrontab=====$error=========");
    });

    // await AwesomeNotificationService.instance.createScheduleNotification(
    //     NotificationScheduleModel(
    //         startTime: convertedStartTime,
    //         endTime: convertedEndTime,
    //         duration: int.parse(notificationDuration)));
    print("Notification Created");
  } else {
    print("Time Before Else ");
  }
} else {
  print("Background Proccess run");
  print("Background Task -> Else Statement");
}

enter image description here

Although, then() method is working it is not sending notification. I have already initialized Awesome Notifications in my main.dart before runApp().

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hammad Pervez
  • 316
  • 4
  • 12
  • Check this https://stackoverflow.com/questions/68439793/how-to-keep-get-notification-using-awesome-package-after-app-is-terminated-in-fl – Xone Sep 10 '22 at 04:09

0 Answers0