【问题标题】:Repeating notification - Flutter重复通知 - Flutter
【发布时间】:2021-02-16 03:40:02
【问题描述】:

如何在 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 开发的初学者
  • 我刚刚更新了问题描述......关于火扑的事情,我在与 firebase 集成时遇到了很多麻烦......我不知道为什么,不是代码,而是pod 文件案例...
  • 您不需要使用远程 (PUSH) 通知来进行提醒。计划提醒最好在没有服务器端的情况下使用本地通知创建。因此,如果您使用 flutter_local_notifications 插件,您就走在了正确的道路上。为什么它不起作用 - 可能有几个原因。请检查您是否错过了插件设置中的任何内容 - pub.dev/packages/flutter_local_notifications#-android-setuppub.dev/packages/flutter_local_notifications#-ios-setup

标签: flutter flutter-dependencies


【解决方案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 链接click Here

【讨论】:

    猜你喜欢
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2019-09-14
    相关资源
    最近更新 更多