首页 > 解决方案 > Flutter 中的本地调度通知

问题描述

我想每天在特定时间触发本地通知(由用户选择)。类似于报警类型。在日期选择器的帮助下。用户必须选择日期和时间,它应该每天在该时间推送本地通知。

标签: flutternotificationsalarmmanagerlocalnotification

解决方案


假设你已经预设了 fl​​utter_local_notification

您可以继续执行以下操作

DateTime scheduledTime处理日期时间分配

import 'package:flutter_local_notifications/flutter_local_notifications.dart'as notifs;
Future<void> scheduleNotification(
    {notifs.FlutterLocalNotificationsPlugin notifsPlugin,
    String id,
    String title,
    String body,
    DateTime scheduledTime}) async {
  var androidSpecifics = notifs.AndroidNotificationDetails(
    id, // This specifies the ID of the Notification
    'Scheduled notification', // This specifies the name of the notification channel
    'A scheduled notification', //This specifies the description of the channel
    icon: 'icon',
  );
  var iOSSpecifics = notifs.IOSNotificationDetails();
  var platformChannelSpecifics = notifs.NotificationDetails(
      androidSpecifics, iOSSpecifics);
  await notifsPlugin.schedule(0, title, "Scheduled notification",
      scheduledTime, platformChannelSpecifics); // This literally schedules the notification

}

用户将如何选择日期(在特定时间每天应触发多少天本地通知

您可以使用日期时间选择器来选择日期,如果它是一次发生的通知

如果它必须在不同的日期同时发生,您可以使用工作管理器并每 24 小时运行一次,并在没有频率结束后结束服务

检查这篇博文


推荐阅读