flutter - Flutter 在指定的时间范围内创建多个每周本地通知
问题描述
对于我即将推出的健身应用程序,用户应该能够在周一和周日之间选择多天,选择应该显示通知的小时数和周数。例如:我选择星期四和星期五,下午 2 点和 6 周。所以我应该在周四和周五下午 2 点看到通知,持续 6 周。
我正在使用“flutter_local_notifications 0.8.3”插件,但它似乎没有我需要的功能,如果您知道像我上面写的那样显示通知的任何不同方法,请随时告诉我。
编辑:
我用for循环设置的预定通知的代码。但这并不是真正的解决方案
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);
}
每周通知代码
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);
}
给定的问题是我不知道如何在 x 周后停止每周通知
此代码来自插件本身,让您了解该功能的工作原理。我可以指定日期、时间和重复间隔,但不能指定通知应该激活多长时间。
/// 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 ?? ''
});
}
编辑:代码片段
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();
}
}
解决方案
- 首先,您需要找到当前日期,例如 22-04-2020,并且您必须将持续时间(如您希望的 6 周)添加到当天,以获得结束日期,例如 27-05-2020。
- 然后你必须遍历开始日期和结束日期。
- 在循环播放时,您必须记录每个日期并确定它是否是您想要的日期。
- 如果您找到给定日期的所需日期,例如 您的时间为24-04-2020的星期五,则将此日期传递给 scheduleNotification() 并将此日期设置为变量 scheduleNotificationDateTime。
推荐阅读
- python - Python,pip install 卡在收集“module_name”
- python - 无法从 python 连接到 MySQL 服务器 Azure
- reactjs - 100vh 的问题,内容大于我的浏览器窗口
- java - 搜索算法中的平均比较次数
- r - 基于R中特定类别的其他列以百分比计算列
- java - 如何在 Spring Boot 中过滤所有数据库操作的某些记录
- javascript - 如果设备不支持抗锯齿,getContext with antialias: true 会返回一个有效的上下文吗?
- angular - 如何验证异步自动完成字段中的输入是否匹配?
- html - 在可滚动元素上应用样式
- python - 如何仅返回对象包含来自 XML/JSON URL 的字符串