小编Br0*_*0wn的帖子

Flutter调度本地通知不起作用

我试图在某个时间创建通知,但它不起作用。该代码不会引发任何错误,但设备上不会显示任何通知。我正在使用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 …
Run Code Online (Sandbox Code Playgroud)

flutter flutter-local-notification

4
推荐指数
1
解决办法
2529
查看次数