Flutter How to make daily local notification from started user date?

26 Views Asked by At

I'm trying to make schedule reminders maybe daily, weekly, or monthly as it will be the user's choice, and the start date and time will be also the user's choice my notification works only for the 1st time and when I change the DateTime of the emulator nothing shown? Do I have to add a task manager for that mission?

my code is

  //show schedual notification
  static void showSchedualNotification(int id, String title, String body,
      String startdate, String starttime) async {
    NotificationDetails details = const NotificationDetails(
      android: AndroidNotificationDetails(
        'id 3',
        'Schedual notification',
        channelDescription: 'your channel description',
        importance: Importance.max,
        priority: Priority.high,
        playSound: true,
        enableVibration: true,
      ),
    );

    tz.initializeTimeZones();
    final String currentTimeZone = await FlutterTimezone.getLocalTimezone();
    tz.setLocalLocation(tz.getLocation(currentTimeZone));
    await flutterLocalNotificationsPlugin.zonedSchedule(
      id,
      title,
      body,
      _nextInstanceOf8PM(startdate, starttime),
      details,
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
    );
  }

  static DateTime parseDateString(String dateString) {
    DateFormat format = DateFormat('MMM dd, yyyy');
    return format.parse(dateString);
  }

  static tz.TZDateTime _nextInstanceOf8PM(String startdate, String starttime) {
    var parsedDate = parseDateString(startdate);
    int hour = int.parse(starttime.split(':')[0]);
    int min = int.parse(starttime.split(':')[1]);

    // Create a TZDateTime object representing the scheduled time
    var scheduledDate = tz.TZDateTime(
      tz.local,
      parsedDate.year,
      parsedDate.month,
      parsedDate.day,
      hour,
      min,
    );

    // Get the current time in the local timezone
    var now = tz.TZDateTime.now(tz.local);

    // Adjust the scheduled time to the next occurrence if it's in the past
    if (now.isAfter(scheduledDate)) {
      scheduledDate = scheduledDate.add(const Duration(days: 1));
    }

    return scheduledDate;
  }
0

There are 0 best solutions below