Sar*_* ED 5 flutter flutter-dependencies
如何在 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);
}
Run Code Online (Sandbox Code Playgroud)
该代码在 showMonthlyAtDayTime 下方有(红色)下划线
'等待 flutterLocalNotificationsPlugin.showMonthlyAtDayTime' 行
和“Day.Tuesday”行...
我应该如何修改上面的代码?这样用户每年每个月都会收到一次有关某件事的通知。
小智 2
最新版本中删除了一些方法。
这是我发现使用 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;
}
Run Code Online (Sandbox Code Playgroud)
此代码是从https://pub.dev/packages/flutter_local_notifications提供的示例应用程序复制的
这是 git hub 链接,请点击这里
| 归档时间: |
|
| 查看次数: |
4244 次 |
| 最近记录: |