工作管理器的颤动安排本地通知 [英] flutter schedule local notification by workManager

查看:84
本文介绍了工作管理器的颤动安排本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从后台 dart 代码中安排通知?

例如,使用 executeTask 方法中的 work_manager 插件,就好像我注册每小时运行一次的服务并根据当前位置安排今天的通知.

我试过了,但没有运气.

有人可以帮我吗?

解决方案

是的,可以在工作经理的帮助下安排本地通知.当我在我的小米设备上尝试 Flutter 本地通知插件时,当应用程序关闭(并从最近的活动中清除)时,通知没有显示.所以我找到的唯一解决方案是使用Work Manager flutter插件直接show(),无需调度.

优点

  • 工作管理器帮助我在应用关闭并从最近的应用列表中清除后显示通知.
  • 即使在设备重启后它也能工作

缺点

  • 有时不会显示通知.
  • 无法管理到固定时间的安排.(如果应用程序关闭,android 有时不允许执行任务)
<预><代码>callbackDispatcher() {Workmanager.executeTask((task, inputData) async {如果(LocalNotification.flutterNotificationPlugin == null){等待 LocalNotification.Initializer();}DateTime now = DateTime.now();var 小时 = inputData['小时'];var minute = inputData['minute'];DateTime time = DateTime(now.year, now.month, now.day, now.hour, minute);等待 LocalNotification.ShowOneTimeNotification(time);返回 Future.value(true);});}

触发

扁平按钮(孩子:文本('设置'),onPressed: () 异步 {等待 Workmanager.initialize(callbackDispatcher);等待 Workmanager.registerPeriodicTask(test_workertask",test_workertask",inputData:{data1":value1",data2":value2"},频率:持续时间(小时:1),初始延迟:持续时间(秒:10),existingWorkPolicy: ExistingWorkPolicy.replace);},),

Is it possible to schedule notification from background dart code?

For example, like using work_manager plugin from executeTask method as if I register service that runs every hour and schedule today notification base on current location.

I tried out but no luck.

Can anyone help me with that?

解决方案

Yes, it is possible to schedule Local notification with the help of work manager. When I was trying the Flutter Local Notification Plugin on my Xiaomi device, the notification is not showing when the app is closed ( and cleared from the recent activity). So the only solution I found was to use the Work Manager flutter plugin to show() directly without scheduling.

PROS

  • Work Manager helped me to show notifications after the app closed and cleared from the recent app list.
  • It will work even after the device restart

CONS

  • The notification will not show up sometimes.
  • The scheduling to fixed time cannot be managed. (android sometimes does not allow to execute task if the app is closed)


callbackDispatcher() {
Workmanager.executeTask((task, inputData) async {
if (LocalNotification.flutterNotificationPlugin == null) {
await LocalNotification.Initializer();
}
DateTime now = DateTime.now();
var hour = inputData['hour'];
var minute = inputData['minute'];
DateTime time = DateTime(now.year, now.month, now.day, now.hour, minute);
await LocalNotification.ShowOneTimeNotification(time);
return Future.value(true);
});
}

Trigger

FlatButton(
child: Text('set'),
onPressed: () async {
await Workmanager.initialize(callbackDispatcher);
await Workmanager.registerPeriodicTask(
"test_workertask", "test_workertask",
inputData: {"data1": "value1", "data2": "value2"},
frequency: Duration(hours: 1),
initialDelay: Duration(seconds: 10),
existingWorkPolicy: ExistingWorkPolicy.replace);
},
),

这篇关于工作管理器的颤动安排本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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