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