[英]flutter schedule local notification by workManager
是否可以从后台 dart 代码安排通知?
例如,就像使用executeTask
方法中的work_manager
插件一样,就好像我注册了每小时运行的服务并根据当前位置安排今天的通知一样。
我试过但没有运气。
任何人都可以帮助我吗?
是的,可以在工作经理的帮助下安排本地通知。 当我在我的小米设备上尝试 Flutter 本地通知插件时,当应用程序关闭(并从最近的活动中清除)时,通知没有显示。 所以我找到的唯一解决方案是直接使用 Work Manager flutter 插件来 show() 无需调度。
优点
缺点
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);
});
}
扳机
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);
},
),
您可以通过重用以下代码段来尝试使用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,
);
为了安排通知工作,您应该在下面添加行。 解决方案是这样的:在 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.