首页 > 解决方案 > 重复通知 - 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”行...

我应该如何修改上面的代码?这样用户每年每个月都会收到一次有关某事的通知。

标签: flutterflutter-dependencies

解决方案


最新版本中删除了一些方法。

这是我发现每周使用 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 链接点击这里


推荐阅读