[英]flutter schedule local notification by workManager
Is it possible to schedule notification from background dart code?是否可以从后台 dart 代码安排通知?
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.例如,就像使用
executeTask
方法中的work_manager
插件一样,就好像我注册了每小时运行的服务并根据当前位置安排今天的通知一样。
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).
当我在我的小米设备上尝试 Flutter 本地通知插件时,当应用程序关闭(并从最近的活动中清除)时,通知没有显示。 So the only solution I found was to use the Work Manager flutter plugin to show() directly without scheduling.
所以我找到的唯一解决方案是直接使用 Work Manager flutter 插件来 show() 无需调度。
PROS优点
CONS缺点
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);
},
),
You can try with flutter_local_notifications by reusing snippet below:您可以通过重用以下代码段来尝试使用flutter_local_notifications :
import 'dart:async';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
var duration = Duration(hours: 1);
var scheduledNotificationDateTime = DateTime.now().add(duration);
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(
0,
'scheduled title',
'scheduled body',
scheduledNotificationDateTime,
platformChannelSpecifics,
);
In order schedule notifications work you should add lines below.为了安排通知工作,您应该在下面添加行。 Solution is this: add these lines after
</activity>
in AndroidManifest.xml:解决方案是这样的:在 AndroidManifest.xml 中的
</activity>
之后添加这些行:
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.