重复通知 - Flutter

问题描述 投票:0回答:2

如何在 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 下方有(红色)下划线

'等待 flutterLocalNotificationsPlugin.showMonthlyAtDayTime' 行

还有“Day.Tuesday”线路...

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

flutter flutter-dependencies
2个回答
1
投票

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

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


0
投票

您可以在 flutterLocalNotificationPlugin.periodicallyShow 中使用属性 RepeatInterval.weekly 。

您还可以根据您的使用情况将其设置为每分钟、每小时或每天。

这是代码示例:

 Future<void> scheduleNotifications({
    required String description,
    required String coralId,
  }) async {
    await flutterLocalNotificationsPlugin.periodicallyShow(
      0,
      'repeating coral id: $coralId',
      'repeating description: $description',
      RepeatInterval.weekly,
      NotificationDetails(
        android: _androidNotificationDetails,
      ),
      androidAllowWhileIdle: true,
    );
    // await flutterLocalNotificationsPlugin.zonedSchedule(
    //   0,
    //   "Scheduled Title",
    //   description,
    //   tz.TZDateTime.now(tz.local).add(
    //     const Duration(minutes: 1),
    //   ),
    //   NotificationDetails(android: _androidNotificationDetails),
    //   androidAllowWhileIdle: true,
    //   uiLocalNotificationDateInterpretation:
    //       UILocalNotificationDateInterpretation.absoluteTime,
    //   payload: coralId,
    //   matchDateTimeComponents: DateTimeComponents.dayOfMonthAndTime,
    // );
  }

© www.soinside.com 2019 - 2024. All rights reserved.