Flutter:预定的通知日期时间 [英] Flutter: Scheduled notification datetime

查看:104
本文介绍了Flutter:预定的通知日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将Flutter的插件date_time_picker与本地通知集成.

I'm looking to integrate Flutter's plugin date_time_picker with local notification.

因此,当我选择日期和时间时,我还会安排一个通知.

So when I select a date&time I also schedule a notification.

这是我的代码.您能帮我吗?现在我没有任何错误,但是代码无法正常工作.

Here's my code..could you help me? Now I don't have any error but code is not working.

 class test1 extends StatefulWidget {
  @override
  _test1State createState() => _test1State();
}

class _test1State extends State {
  FlutterLocalNotificationsPlugin fltrNotification;
  String _selectedParam;
  String task;
  int val;

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

  Future scheuleAtParticularTime(DateTime timee) async {
    // var time = DateTime(timee.day, timee.month, timee.year, timee.hour,
    //     timee.minute, timee.second);
    var time =
        DateTime(timee.year, timee.month, timee.day, timee.hour, timee.minute);

    var scheduledNotificationDateTime =
        new DateTime.now().add(new Duration(seconds: 5));
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
        'your other channel id',
        'your other channel name',
        'your other channel description');
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
    NotificationDetails platformChannelSpecifics = new NotificationDetails(
        android: androidPlatformChannelSpecifics,
        iOS: iOSPlatformChannelSpecifics);
    // tz.initializeTimeZones();
    // tz.setLocalLocation(tz.getLocation('Italy'));
    await fltrNotification.schedule(1, 'scheduled title', 'scheduled body',
        scheduledNotificationDateTime, platformChannelSpecifics);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            FlatButton(
              onPressed: () {
                DatePicker.showDateTimePicker(context, showTitleActions: true,
                    onChanged: (date) {
                  print('change $date');
                }, onConfirm: (date) {
                  print('confirm $date');
                  scheuleAtParticularTime(DateTime.fromMillisecondsSinceEpoch(
                      date.millisecondsSinceEpoch));
                }, currentTime: DateTime.now(), locale: LocaleType.it);
              },
              child: Text(
                'Select time for notification arrival',
                style:
                    TextStyle(color: Colors.black, fontWeight: FontWeight.w900),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future notificationSelected(String payload) async {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        content: Text("Notification Clicked $payload"),
      ),
    );
  }
}

谢谢!

ps ..我插入了完整的代码,通知可以正常运行,但时间是随机的(从选择器中设置大约5秒后)

ps.. I've insert full code, notifications works but at random time (about 5 second after I set it from the picker)

推荐答案

好,我发现了问题:

等待fltrNotification.schedule(1,'scheduled title','scheduled body',timee,platformChannelSpecifics);

代替

await fltrNotification.schedule(1, 'scheduled title', 'scheduled body',
        scheduledNotificationDateTime, platformChannelSpecifics);

这篇关于Flutter:预定的通知日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆