我正在尝试显示一个包含检查24小时的条件信息的通知。我正在与工作管理器一起使用flutter_local_notification,并且我将在预定时间收到通知。但是,它没有按预期显示消息。这是我的代码:
class NotificationServices{
static AndroidNotificationDetails androidSettings;
static IOSNotificationDetails iosSettings;
static FlutterLocalNotificationsPlugin fltrNotification;
static initializer(){
fltrNotification = FlutterLocalNotificationsPlugin();
androidSettings = AndroidNotificationDetails(
'Expiry Id', 'Expiry', 'Expired',
playSound: true, enableVibration: true,
importance: Importance.max, priority: Priority.max);
iosSettings = IOSNotificationDetails();
var androidInitialize = new AndroidInitializationSettings('exercise_icon');
var iOSInitialize = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
android: androidInitialize, iOS: iOSInitialize);
fltrNotification.initialize(initializationSettings);
}
static showNotification() async {
var notifDetails = NotificationDetails(android: androidSettings, iOS: iosSettings);
List<Member> memList = await checkDate();
if(memList.isNotEmpty){
memList.forEach((member) async{
var scheduledTime = DateTime.parse(member.date);
final timeZone = TimeZone();
String timeZoneName = await timeZone.getTimeZoneName();
final location = await timeZone.getLocation(timeZoneName);
final scheduledDate = tz.TZDateTime.from(scheduledTime, location).add(Duration(minutes: 35));
fltrNotification.show(
member.id,
'Subscription Notification',
'${member.surname} ${member.firstName}\'s subscription has expired.',
notifDetails
);
// await fltrNotification.zonedSchedule(
// member.id,
// 'Subscription Notification',
// '${member.surname} ${member.firstName}\'s subscription has expired.',
// scheduledDate,
// notifDetails,
// uiLocalNotificationDateInterpretation:
// UILocalNotificationDateInterpretation.absoluteTime,
// androidAllowWhileIdle: true);
});
}
}
}这是初始化workmanager的main.dart:
void callbackDispatcher() {
Workmanager.executeTask((taskName, inputData) {
NotificationServices.initializer();
NotificationServices.showNotification();
return Future.value(true);
});
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
Workmanager.initialize(
callbackDispatcher,
isInDebugMode: true,
);
Workmanager.registerPeriodicTask(
'4',
'simplePeriodicTask',
frequency: Duration(minutes: 15),
);
runApp(MyApp());
}
Future<List<Member>>checkDate() async {
MemberServices memServices = MemberServices();
List<Member> memList = List<Member>();
DateTime now = DateTime.now();
List<Member> membersList = await memServices.getAllMembers();
membersList.forEach((member) async {
DateTime expiryDate = DateTime.parse(member.date);
if (now.isAfter(expiryDate)) {
member = Member(
id: member.id,
surname: member.surname,
firstName: member.firstName,
date: member.date,
active: 'false');
await memServices.updateMember(member);
memList.add(member);
}
});
return memList;
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Color(0xFF070707),
accentColor: Colors.purple[800]
),
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}这是我得到的结果

但是没有workmanager,它工作得很好。我需要一个环境。
编辑:我尝试在callbackDispatcher(){}中打印一条消息,但没有打印。这是没有工作管理器时的样子:

发布于 2021-02-20 04:31:57
我刚刚解决了这个问题,这实际上是相当短的时间。Workmanager方法是未来的,我没有意识到这一点,所以我没有等待它们,其次,我的代码调用只在满足特定条件时显示通知,并且该条件每天午夜才满足,所以我暂时忽略了该条件。第三,在void main之前,我做了回调和showNotification。现在我的代码如下所示
const simplePeriodicTask = 'simplePeriodicTask';
MemberServices memberServices = MemberServices();
Future _showNotification(fltrNotification) async {
var androidSettings = AndroidNotificationDetails(
'Expiry Id', 'Expiry', 'Expired',
playSound: true, enableVibration: true,
importance: Importance.max, priority: Priority.max);
var iosSettings = IOSNotificationDetails();
var notifDetails = NotificationDetails(android: androidSettings, iOS: iosSettings);
await fltrNotification.show(
1,
'Subscription Notification',
'Faith\'s subscription has expired.',
notifDetails
);
}
Future<void> main() async{
WidgetsFlutterBinding.ensureInitialized();
print('here ****************************************************************');
await Workmanager.initialize(
callbackDispatcher,
isInDebugMode: true,
);
print('got here three');
await Workmanager.registerPeriodicTask(
'5',
simplePeriodicTask,
frequency: Duration(minutes: 15),
initialDelay: Duration(seconds: 10)
);
runApp(MyApp());
}
void callbackDispatcher(){
print('dispatch -----------------------------------------------------------------');
Workmanager.executeTask((taskName, inputData) async{
FlutterLocalNotificationsPlugin fltrNotification = FlutterLocalNotificationsPlugin();
var androidInitialize = new AndroidInitializationSettings('exercise_icon');
var iOSInitialize = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
android: androidInitialize, iOS: iOSInitialize);
fltrNotification = new FlutterLocalNotificationsPlugin();
fltrNotification.initialize(initializationSettings);
List<Member> memList = await memberServices.checkDate();
if(memList.isNotEmpty){
print('if memList ---------------------------------------------------------');
for(Member m in memList){
var scheduledTime = DateTime.now();
final timeZone = TimeZone();
String timeZoneName = await timeZone.getTimeZoneName();
final location = await timeZone.getLocation(timeZoneName);
final scheduledDate = tz.TZDateTime.from(scheduledTime, location).add(Duration(seconds: 20));
print('here ==================================================================');
// await fltrNotification.zonedSchedule(
// m.id,
// 'Subscription Notification',
// '${m.surname} ${m.firstName}\'s subscription has expired.',
// scheduledDate,
// notifDetails,
// uiLocalNotificationDateInterpretation:
// UILocalNotificationDateInterpretation.absoluteTime,
// androidAllowWhileIdle: true);
}
}
_showNotification(fltrNotification);
print('execute ___________________________________________________________');
return Future.value(true);
});
}我必须打印检查点才能知道我的代码在哪里出错。现在我的通知看起来像这样,这就是它应该看起来的样子;

https://stackoverflow.com/questions/66275901
复制相似问题