1

我正在尝试Local Notifications在 Flutter 应用程序中进行设置,并且需要每天触发一次通知,并且需要从中读取通知文本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 分钟打印一次。

请帮忙 !!

谢谢

4

0 回答 0