颤振|本地通知: ZonedSchedule 只触发一次

时间:2021-07-10 07:49:47

标签: flutter localnotification flutter-local-notification

我正在尝试在 Flutter 应用程序中设置 Local Notifications,并且需要每天触发一次通知并且需要从 SQLite 读取通知文本。

为了测试,我试图每 2 分钟运行一次,看看它是否从 SQLite 正确获取数据,但它似乎只运行一次...

await flutterLocalNotificationsPlugin.zonedSchedule(
      notificationID,
      "Dynamic Data",
    await getData(),
       _nextInstanceOfTime(initialHour,initialMin),// have defined an initial hr and min to start the notification
       const NotificationDetails(
          android: AndroidNotificationDetails('1', 'test', 'test')),
      androidAllowWhileIdle: true,
      uiLocalNotificationDateInterpretation:
      UILocalNotificationDateInterpretation.absoluteTime,
      matchDateTimeComponents: DateTimeComponents.time
  );

tz.TZDateTime _nextInstanceOfTime(int hour, int minutes) {
Duration offsetTime= DateTime.now().timeZoneOffset;
tz.TZDateTime scheduledDate;
tz.TZDateTime currentTime;

if (offsetTime.isNegative) {
  scheduledDate = tz.TZDateTime.local(DateTime.now().year,DateTime.now().month,DateTime.now().day,hour,minutes)
      .add(offsetTime);
  currentTime = tz.TZDateTime.local(DateTime.now().year,DateTime.now().month,DateTime.now().day,DateTime.now().hour,DateTime.now().minute)
      .add(offsetTime);
}
else {
  scheduledDate = tz.TZDateTime.local(DateTime.now().year,DateTime.now().month,DateTime.now().day,hour,minutes)
      .subtract(offsetTime);
  currentTime = tz.TZDateTime.local(DateTime.now().year,DateTime.now().month,DateTime.now().day,DateTime.now().hour,DateTime.now().minute)
      .subtract(offsetTime);
}


if (scheduledDate.isBefore(currentTime)) {
  scheduledDate = scheduledDate.add(const Duration(days: 1));
}
scheduledDate = scheduledDate.add(const Duration(minutes: 2));//->>> WANTING TO TEST FIRING IT EVERY 2 MINS
print("next notification:$scheduledDate"); // ->> THIS PRINTS ONLY ONCE
return scheduledDate;

}

getData() 从 SQLite 中获取随机字符串

print("next notification:$scheduledDate") 只打印一次,而不是根据需要每 2 分钟打印一次。

请帮忙!!

谢谢

0 个答案:

没有答案