Br0*_*0wn 4 flutter flutter-local-notification
我试图在某个时间创建通知,但它不起作用。该代码不会引发任何错误,但设备上不会显示任何通知。我正在使用flutter_local_notifications
生成通知的代码:
Future<void> showNotification(
int hour, int id, String title, String body) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
_convertTime(hour),
const NotificationDetails(
android: AndroidNotificationDetails('main_channel', 'Main Channel',
channelDescription: "ashwin",
importance: Importance.max,
priority: Priority.max),
iOS: IOSNotificationDetails(
sound: 'default.wav',
presentAlert: true,
presentBadge: true,
presentSound: true,
),
),
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true,
);
}
Run Code Online (Sandbox Code Playgroud)
_converTime函数代码:
TZDateTime _convertTime(int hour) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduleDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, 47);
return scheduleDate;
}
Run Code Online (Sandbox Code Playgroud)
该函数的结果是正确的,如下:2022-07-14 12:47:00.000Z
但是,如果我不使用此函数,而是将其更改为
Future<void> showNotification(
int hour, int id, String title, String body) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
// _convertTime(hour), // this does not work
tz.TZDateTime.now(tz.local).add(Duration(seconds: 1)), // this work
const NotificationDetails(
android: AndroidNotificationDetails('main_channel', 'Main Channel',
channelDescription: "ashwin",
importance: Importance.max,
priority: Priority.max),
// iOS details
iOS: IOSNotificationDetails(
sound: 'default.wav',
presentAlert: true,
presentBadge: true,
presentSound: true,
),
),
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true,
);
}
Run Code Online (Sandbox Code Playgroud)
我不明白会发生什么。我感谢您提前提供的任何帮助。
试试这个,如果你不想要的话,删除一些属性......
Future zonedScheduleNotification(String note, DateTime date, occ) async {
// IMPORTANT!!
//tz.initializeTimeZones(); --> call this before using tz.local (ideally put it in your init state)
int id = math.Random().nextInt(10000);
log(date.toString());
log(tz.TZDateTime.parse(tz.local, date.toString())
.toString());
try {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
occ,
note,
tz.TZDateTime.parse(tz.local, date.toString()),
NotificationDetails(
android: AndroidNotificationDetails(
'your channel id', 'your channel name',
channelDescription: 'your channel description',
largeIcon: DrawableResourceAndroidBitmap("logo"),
icon: "ic_launcher",
playSound: true,
sound: RawResourceAndroidNotificationSound('bell_sound')),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
return id;
} catch (e) {
log("Error at zonedScheduleNotification----------------------------$e");
if (e ==
"Invalid argument (scheduledDate): Must be a date in the future: Instance of 'TZDateTime'") {
Fluttertoast.showToast(msg: "Select future date");
}
return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2529 次 |
| 最近记录: |