我遇到的问题是这个错误。
发生异常。 PlatformException(PlatformException(INVALID_ARGUMENTS,计划 cron 表达式无效,arguments.invalid.crontabExpression,null)) !D/*
仅当我尝试在 crontabExpression 上指定日期时才会发生这种情况。
示例:
Future<void> customNotification({
required int idNotification,
required int idMedicine,
required int listLenght,
required String groupKey,
required DateTime endingDate,
required DateTime startDate,
required String myCron,
required String medicareName,
required int numberOfMedicine,
required String medicineType,
}) async {
log('my id is $idNotification , my medicine id is $idMedicine, myCron is $myCron ,my starting date is ${startDate.toIso8601String()} my date is ending date ${endingDate.toIso8601String()} ');//debug
await AwesomeNotifications().createNotification(
content: NotificationContent(
notificationLayout: NotificationLayout.BigPicture,
duration: const Duration(seconds: 1),
id: idNotification,
groupKey: groupKey,
color: Colors.lightBlue,
channelKey: 'medicine_channel',
actionType: ActionType.Default,
title: 'Hi, it\'s time for your medication',
body:'Take your medication named $medicareName at quantity \n $numberOfMedicine $medicineType.$idNotification',//debug
autoDismissible: true,
payload: {'$idMedicine': '$listLenght'},
),
schedule: NotificationAndroidCrontab(
allowWhileIdle: true,
expirationDateTime: endingDate,
initialDateTime: startDate,
crontabExpression: myCron,
preciseAlarm: true),
actionButtons: <NotificationActionButton>[
NotificationActionButton(
color: Colors.blue,
key: 'take',
label: 'take',
actionType: ActionType.SilentAction),
NotificationActionButton(
isDangerousOption: true,
color: Colors.red,
key: 'cancel',
label: 'cancel',
actionType: ActionType.SilentAction),
],
);
}
如果我在 myCron 值中放入类似
0 44 2 * * ? *
的内容
它工作正常,但如果我做类似的事情:
44 2 * * 1,2,5
0 44 2 * * 1,2,5
? 44 2 * * 1,2,5
? 44 2 * * MON,TUE,FRI
44 2 * * MON,TUE,FRI
? 44 2 * * 1,2,5 *
0 44 2 * * 1,2,5 ? (and more)
它抛出该错误(在开始时)。
我想要的是创建一个 cron,在每个星期一、星期六或至少每个星期一当前时间发生通知。正如上面所示,我尝试了很多方法但没有任何效果。
有办法解决这个问题吗?
感谢您的宝贵时间。
我已经解决了你的问题。
以下是执行任务的详细信息:
我使用 Awesome_notifications: ^0.9.3+1 pub 每周一和周六上午 9 点执行通知。
以下是步骤:
await AwesomeNotifications().requestPermissionToSendNotifications();
await AwesomeNotifications().showAlarmPage();
注意:如果未添加此权限,则不会触发通知。
static Future<void> repeatMinuteNotificationOClock() async {
bool isAllowed = await AwesomeNotifications().isNotificationAllowed();
if (!isAllowed) isAllowed = await displayNotificationRationale();
if (!isAllowed) return;
String utcTimeZone =
await AwesomeNotifications().getLocalTimeZoneIdentifier();
print(utcTimeZone);
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: -1,
channelKey: 'scheduled',
title: 'Notification at exactly every single second',
body:
'This notification was schedule to repeat at every single second at clock.',
notificationLayout: NotificationLayout.BigPicture,
bigPicture: 'https://storage.googleapis.com/cms-storage-bucket/d406c736e7c4c57f5f61.png',
payload: {
'actPag': 'myAct',
'actType': 'food',
'username': "Test"
}),
schedule: NotificationAndroidCrontab(
allowWhileIdle: true,
crontabExpression: '0 0 9 ? * MON,SAT *',
preciseAlarm: true,
repeats: true,timeZone: utcTimeZone)); }
对于重复通知,我必须设置 cron 表达式: crontabExpression: *'0 0 9 ? * 周一、周六 ',
其中 0 代表第二个 0 代表分钟 9为小时(支持24小时格式) ?适合任何一天
执行上述步骤后,每周一和周六早上 9 点就会收到通知。