Flutter计划本地通知不起作用

kkbh8khc  于 2023-06-30  发布在  Flutter
关注(0)|答案(2)|浏览(137)

我试图在某个时间创建通知,但它不起作用。代码不会抛出任何错误,但不会在设备上显示任何通知。我用的是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,
);
}

_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;
}

函数的结果正确,如下所示: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,
);
}

我不明白会发生什么事。我很感激任何提前帮助。

hfyxw5xn

hfyxw5xn1#

试试这个,如果你不想要的话,删除一些属性……

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;
        }
      }
vfh0ocws

vfh0ocws2#

再试一次

tz.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;
}

相关问题