简体   繁体   English

Flutter schedule 本地通知不起作用

[英]Flutter schedule local notification does not work

I am trying to create a notification at a certain time but it is not working.我正在尝试在某个时间创建通知,但它不起作用。 The code does not throw any error but no notification is displayed on the device.该代码不会引发任何错误,但设备上不会显示任何通知。 I am using flutter_local_notifications我正在使用flutter_local_notifications

Code that generates the notification:生成通知的代码:

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 function code: _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;
}

The result of the function is correct and is as follows: 2022-07-14 12:47:00.000Z函数结果正确,如下: 2022-07-14 12:47:00.000Z

However, if instead of using this function, I change it to但是,如果我不使用此功能,而是将其更改为

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,
);
}

I don't understand what could be happening.我不明白会发生什么。 I appreciate any help in advance.我提前感谢任何帮助。

try this one, remove some properties if you don't want them...试试这个,如果您不想要它们,请删除一些属性...

 Future zonedScheduleNotification(String note, DateTime date, occ) async {
        // tz.TZDateTime.parse(location, formattedString)
        int id = math.Random().nextInt(10000);
        log(date.toString());
        log(tz.TZDateTime.parse(tz.getLocation("Asia/Kolkata"), date.toString())
            .toString());
        try {
          await flutterLocalNotificationsPlugin.zonedSchedule(
            id,
            occ,
            note,
            tz.TZDateTime.parse(tz.getLocation("Asia/Kolkata"), 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;
        }
      }

Try this again.再试一次。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM