Flutter:本地通知调度

问题描述

我正在尝试从用户选择的日期和时间设置预定的警报通知,我在下面的代码中使用了 showDatePicker

DateTime _selectedDateAndTime;

  Future _selectDayAndTimeL(BuildContext context) async {
    DateTime _selectedDay = await showDatePicker(
        context: context,initialDate: DateTime.Now(),firstDate: DateTime(2021),lastDate: DateTime(2030),builder: (BuildContext context,Widget child) => child);

    TimeOfDay _selectedTime = await showTimePicker(
      context: context,initialTime: TimeOfDay.Now(),);

    if (_selectedDay != null && _selectedTime != null) {
      //a little check
    }
    setState(() {
      _selectedDateAndTime = DateTime(
        _selectedDay.year,_selectedDay.month,_selectedDay.day,_selectedTime.hour,_selectedTime.minute,);
      // _selectedDate = _selectedDay;
    });
    // print('...');
  }

选择日期和时间后,该值的格式如下图

enter image description here

现在我希望能够使用选择中的值设置计划通知但不知道该怎么做...我已经安装了 Flutter_Local_Notification 并将其导入到我的 main.dart,已在manifest 文件,并尝试初始化插件如下

FlutterlocalnotificationsPlugin fltrNotification;
  String _selectedParam;
  int val;

  @override
  void initState() {
    super.initState();
    var androidInitilize = new AndroidInitializationSettings('app_icon');
    var iOSinitilize = new IOSInitializationSettings();
    var initilizationsSettings =
        new InitializationSettings(androidInitilize,iOSinitilize);
    fltrNotification = new FlutterlocalnotificationsPlugin();
    fltrNotification.initialize(initilizationsSettings,onSelectNotification: notificationSelected);
  }

而且我还添加了 app_icon.png 到我的 drawable 文件

我曾尝试遵循一些有关如何操作的教程,但其中大多数只展示了如何使用秒设置网络化,但对于我自己的项目,我想设置特定日期、小时和分钟的时间表

请问我怎样才能做到这一点?

解决方法

你可以使用这个我的助手类

class NotificationPlugin {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

  var initializationSettings;

  NotificationPlugin._() {
    init();
  }

  init() async {
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    if (Platform.isIOS) {
      _requestIOSPermission();
    }
    initializePlatformSpecifics();
  }

  initializePlatformSpecifics() {
    var initializationSettingsAndroid = AndroidInitializationSettings(
        'mipmap/ic_launcher'); // <- default icon name is @mipmap/ic_launcher
    var initializationSettingsIOS =
        IOSInitializationSettings(onDidReceiveLocalNotification: (int a,String b,String c,d) {});
    var initializationSettings =
        InitializationSettings(initializationSettingsAndroid,initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,onSelectNotification: (String s) {});

    initializationSettings =
        InitializationSettings(initializationSettingsAndroid,initializationSettingsIOS);
  }

  _requestIOSPermission() {
    flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()
        .requestPermissions(alert: false,badge: true,sound: true);
  }

  setOnNotificationClick(Function onNotificationClick) async {
    await flutterLocalNotificationsPlugin.initialize(
      initializationSettings,onSelectNotification: (payload) async {
        onNotificationClick(payload);
      },);
  }

  Future<void> showNotification(
      {@required int id,@required String title,@required String body}) async {
    var androidChannelSpecifics = AndroidNotificationDetails(
      'CHANNEL_ID','CHANNEL_NAME','CHANNEL_DESCRIPTION',importance: Importance.High,priority: Priority.High,);
    var iosChannelSpecifics = IOSNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
      androidChannelSpecifics,iosChannelSpecifics,);
    await flutterLocalNotificationsPlugin.show(id,title,body,platformChannelSpecifics,payload: id.toString());
  }

  Future<void> showScheduledNotification(
      {@required int id,@required String body,@required String date}) async {
    var scheduledNotificationDateTime = DateTime.parse(date);

    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'your other channel id','your other channel name','your other channel description');
    var iOSPlatformChannelSpecifics = IOSNotificationDetails();
    NotificationDetails platformChannelSpecifics =
        NotificationDetails(androidPlatformChannelSpecifics,iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.schedule(
        id,'$title','  $body',scheduledNotificationDateTime,androidAllowWhileIdle: true);
  }

  Future<void> removeNotifications() async {
    await flutterLocalNotificationsPlugin.cancelAll();
  }
}

NotificationPlugin notificationPlugin = NotificationPlugin._();

然后你可以打电话

await notificationPlugin.showScheduledNotification(
                id: 123,title:"fancy title",body: "data",date: yourDate,);

///////////////////////////////// 如果你不会使用辅助类 这正是您所需要的

var scheduledNotificationDateTime = DateTime.parse(date); // replace whith your date

var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'your other channel id','your other channel description');
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
NotificationDetails platformChannelSpecifics =
    NotificationDetails(androidPlatformChannelSpecifics,iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.schedule(
    id,androidAllowWhileIdle: true);