因此,我有一个应用程序,它提醒用户在特定的日期间隔内每天在特定的时间服用药物。例如,用户可以选择从2020年9月16日到2020年9月18日的某一天的某个时间接收通知
我的方法是:我使用带有showDailyAtTime()函数的flutter_local_notifications包来调度通知。然而,我面临的问题是,假设我不再打开应用程序,没有办法取消预定的通知,因此,即使在指定的日期范围之后,通知也会弹出。我希望通知是离线的,所以Firebase似乎不是一个选择。
发布于 2020-09-16 17:53:34
你可以用FlutterLocalNotificationsPlugin来解决这个问题。
方法是在每次启动应用程序时调用rescheduleNotifications方法。在该方法中,删除了所有通知,并设置了下一个通知。例如,在calculateNotificationTimes中,您计算未来30天的所有通知。例如,所有在2020年9月16日至2020年9月18日的通知都是在您选择的时间内发出的。
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
Future<void> rescheduleNotifications() async {
final localNotificationsPlugin = FlutterLocalNotificationsPlugin();
const initializationSettings = InitializationSettings(AndroidInitializationSettings('app_icon'), IOSInitializationSettings());
const androidChannelSpecifics = AndroidNotificationDetails('your channel id', 'your channel name', 'your channel description');
const iOSNotificationDetails = IOSNotificationDetails();
const notificationsDetails = NotificationDetails(androidChannelSpecifics, iOSNotificationDetails);
await localNotificationsPlugin.initialize(initializationSettings);
await localNotificationsPlugin.cancelAll();
// Calculate the next notifications.
final notificationTimes = calculateNotificationTimes();
var _currentNotificationId = 0;
for (final time in notificationTimes) {
localNotificationsPlugin.schedule(
_currentNotificationId++,
"It's time to take your medicine.",
'Take the red pill',
time,
notificationsDetails,
androidAllowWhileIdle: true,
);
}
}在iOS上,您只能启用64个通知。这种方法在iOS上的缺点是,如果用户在64次通知后没有打开应用程序,则不会显示任何通知。我认为这很好,因为用户似乎不再使用这个应用程序了。
未测试代码。
https://stackoverflow.com/questions/63917079
复制相似问题