2

For my upcoming Fitness-app the user should be able to pick multiple days between monday and sunday, choose the hour and the amount of weeks for which the notification should be displayed. For example: I choose thursday and friday, 2 pm and 6 weeks. So I should see the notification on thursday and friday on 2 pm for 6 weeks.

I am working with the 'flutter_local_notifications 0.8.3' Plugin, but it does not seem to have the functionality that I need, if you know any different approach to display notifications like I wrote above, feel free to tell me.

Edit:

code for the scheduled notification which I set with an for-loop. But its not really a solution

Future<void> scheduleNotification(int id,{int, days,int hour, int minute, int second}) async {
  var scheduledNotificationDateTime =
  DateTime.now().add(Duration(days: days));
  var vibrationPattern = Int64List(4);
  vibrationPattern[0] = 0;
  vibrationPattern[1] = 1000;
  vibrationPattern[2] = 5000;
  vibrationPattern[3] = 2000;

  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
      'your other channel id',
      'your other channel name',
      'your other channel description',
      icon: 'app_icon',
      largeIcon: 'app_icon',
      largeIconBitmapSource: BitmapSource.Drawable,
      vibrationPattern: vibrationPattern,
      enableLights: true,
      color: const Color.fromARGB(255, 255, 0, 0),
      ledColor: const Color.fromARGB(255, 255, 0, 0),
      ledOnMs: 1000,
      ledOffMs: 500);
  var iOSPlatformChannelSpecifics =
  IOSNotificationDetails(sound: "slow_spring_board.aiff");
  var platformChannelSpecifics = NotificationDetails(
      androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.schedule(
      id,
      'scheduled title',
      'scheduled body',
      scheduledNotificationDateTime,
      platformChannelSpecifics);
}

Code for the weekly notification

var androidPlatformChannelSpecifics = AndroidNotificationDetails(
      'show weekly channel id',
      'show weekly channel name',
      'show weekly description');
  var iOSPlatformChannelSpecifics = IOSNotificationDetails();
  var platformChannelSpecifics = NotificationDetails(
      androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(
      0,
      title,
      'Weekly notification shown on Monday at approximately ${_toTwoDigitString(time.hour)}:${_toTwoDigitString(time.minute)}:${_toTwoDigitString(time.second)}',
      Day.values[value],
      time,
      platformChannelSpecifics);
}

The given problem is that I do not know how to stop the weekly Notifcation after x Weeks

This Code is from the Plugin itself, to give you an insight how the function works. I can specify the day, the time and the repeatinterval but not for how long the notification should be active.

 /// Shows a notification on a daily interval at the specified time
  Future<void> showWeeklyAtDayAndTime(int id, String title, String body,
      Day day, Time notificationTime, NotificationDetails notificationDetails,
      {String payload}) async {
    _validateId(id);
    var serializedPlatformSpecifics =
        _retrievePlatformSpecificNotificationDetails(notificationDetails);
    await _channel.invokeMethod('showWeeklyAtDayAndTime', <String, dynamic>{
      'id': id,
      'title': title,
      'body': body,
      'calledAt': DateTime.now().millisecondsSinceEpoch,
      'repeatInterval': RepeatInterval.Weekly.index,
      'repeatTime': notificationTime.toMap(),
      'day': day.value,
      'platformSpecifics': serializedPlatformSpecifics,
      'payload': payload ?? ''
    });
  }

EDIT: code snippet

 Future<void> scheduleActivityList() async {
    for (int i = 0; i < _savedActivityList.length; i++) {
      if ((_savedActivityList[i].startDate
          .difference(DateTime.now())
          .inMinutes <= 10080) && (_savedActivityList[i].scheduled == false)) {
        _savedActivityList[i].scheduled = true;
        scheduleNotification(await _getNextAvailableId(),_savedActivityList[i].startDate
            .difference(DateTime.now())
            .inMinutes, _savedActivityList[i].title);


        if (_savedActivityList[i].startDate  /// deletes past notification
            .difference(DateTime.now())
            .inMinutes <= 0){
          _savedActivityList.removeAt(i);
        }
      }
      await saveActivityList();
    }
  }
DavidGetter
  • 349
  • 3
  • 12
  • post a code snippet please, let the community know what did you done so far for solve the issue. – Kiran Maniya Sep 25 '19 at 17:27
  • Hello Kiran, as mentioned I am fairly limited with the Plugin, you can view the available functions here: https://pub.dev/packages/flutter_local_notifications I did nothing more than schedule the notifications that i needed with an for-loop. This way i would create far too many notifications and I believe I-OS only accepts 60 at a time. My question is rather about the approach. There is also a way to display weekly notifications on a specified time and day but I really do not know how to stop the Notification after exactly 6 weeks for example. – DavidGetter Sep 26 '19 at 09:24
  • Hi @DavidGetter did you find a solution for this? I've run into exactly this problem myself – Giles Correia Morton May 11 '20 at 10:35
  • 1
    @GilesCorreiaMorton I had somewhat of a solution, the idea was that each of my Activity objects had a isScheduled attribute. Because of the scheduling limit I only scheduled all the activities for the next 7 days. So on app initialization I went through my activites and scheduled them if they were isScheduled == false and if they were less than 7 days in the future. I worked with the local notification method where you can schedule one time. I will edit my question and give you the code snippet, I don't recall everything but I hope it helps you. – DavidGetter May 11 '20 at 10:53
  • @DavidGetter that is great thank you so much. I will give this a try, your described method should work well for my use case :) – Giles Correia Morton May 11 '20 at 11:05

1 Answers1

1
  1. First of all you need to find the current date say 22-04-2020 and you have to add the duration as like your wish say 6 weeks to the current day to get end date say 27-05-2020.
  2. Then you have to loop through the start date and end date.
  3. While looping you have to take each date and find out whether is it your desired day or not.
  4. If you find your desired day for the given date, for example Friday for the date 24-04-2020 with your time, then pass this date to the scheduleNotification() and set this date to the variable scheduledNotificationDateTime.
Aneesh Raj
  • 11
  • 1