flutter - 重复通知 - Flutter
问题描述
如何在 Flutter 中安排每月或每周通知?目的是为了提醒用户某事......
这是我的代码
void showMonthlyAtDayTime() async {
//var scheduledNotificationDateTime = DateTime.now().add(Duration(seconds: 10));
var time = Time(12, 6, 0);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'alarm_notif',
'Scheduled notification', // name of the notif channel
'A scheduled notification', // description of the notif channel
icon: 'question',
largeIcon: DrawableResourceAndroidBitmap('question'),
);
var iOSPlatformChannelSpecifics = IOSNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true);
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.showMonthlyAtDayTime(
0,
'Title',
'Body',
Day.Tuesday,
time,
platformChannelSpecifics);
}
代码在 showMonthlyAtDayTime 下方有(红色)下划线
'等待颤振LocalNotificationsPlugin.showMonthlyAtDayTime' 行
和“Day.Tuesday”行...
我应该如何修改上面的代码?这样用户每年每个月都会收到一次有关某事的通知。
解决方案
最新版本中删除了一些方法。
这是我发现每周使用 flutter_local_notifications 重复通知的代码。
Future<void> _scheduleWeeklyTenAMNotification() async {
await notificationsPlugin.zonedSchedule(
0,
'weekly scheduled notification title',
'weekly scheduled notification body',
_nextInstanceOfTenAM(),
const NotificationDetails(
android: AndroidNotificationDetails(
'weekly notification channel id',
'weekly notification channel name',
'weekly notificationdescription'),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime);
}
tz.TZDateTime _nextInstanceOfTenAM() {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, 10);
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
此代码复制自https://pub.dev/packages/flutter_local_notifications提供的示例应用程序
这是 git hub 链接点击这里
推荐阅读
- scip - SCIP:如何确定现有解决方案中变量的当前值是否为整数?
- python - 按钮在 Tkinter Python 应用程序中不起作用
- ramda.js - Ramda - 从一个对象中挑选并返回一个数组
- constraint-programming - Minizinc 模型在类似于 Vehicle Route Problem 的问题中的拾取和交付
- reactjs - 如何将 JSON 从 materialTopTabNavigator 传递到其他屏幕组件反应原生?
- python - PYTHON-DOTENV 错误:没有名为“dotenv”的模块
- python - Python中的Selenium:关闭警报不起作用
- c++ - 如何在两个 boost::multi_arrays (C++) 之间执行数学运算?
- r - For 循环和计数函数 R
- python - Tensorboard 在两个图中显示验证数据和训练数据