Удаление напоминания из базы данных возможно, когда приложение работает в фоновом режиме, но не когда приложение закрыто.
При закрытии я нажимаю кнопку «Готово» в уведомлении и открываю приложение, чтобы проверить, но все еще вижу напоминание, которое следовало удалить.
Вот код моего файла Notification.dart:
Код: Выделить всё
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:nagger/data/reminders_data.dart';
// import 'package:timezone/timezone.dart' as tz;
class NotificationController {
// final notificationsPlugin = AwesomeNotifications();
static ReceivedAction? initialAction;
static void Function()? refreshHomePageCallback;
static Future initializeLocalNotifications() async {
await AwesomeNotifications().initialize(
null,
[
NotificationChannel(
channelKey: '111',
channelName: 'nice_channel_name',
channelDescription: 'Shows Reminder Notification',
)
],
);
initialAction = await AwesomeNotifications()
.getInitialNotificationAction(removeFromActionEvents: false);
}
static void initializeCallback(void Function() func) {
refreshHomePageCallback = func;
}
static Future showNotification(String notifTitle) async {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: 0,
channelKey: '111',
title: notifTitle,
),
actionButtons: [
NotificationActionButton(
key: 'done',
label: 'Done'
)
]
);
}
Future scheduleNotification(
int notifID,
String notifTitle,
DateTime scheduleNotificationDateTime
) async {
return await AwesomeNotifications().createNotification(
content: NotificationContent(
id: notifID,
channelKey: '111',
title: notifTitle,
payload: {
"App name": "Nagger"
},
autoDismissible: false
),
actionButtons: [
NotificationActionButton(
key: 'done',
label: 'Done',
actionType: ActionType.SilentBackgroundAction
),
],
schedule: NotificationCalendar(
year: scheduleNotificationDateTime.year,
month: scheduleNotificationDateTime.month,
day: scheduleNotificationDateTime.day,
hour: scheduleNotificationDateTime.hour,
minute: scheduleNotificationDateTime.minute,
second: scheduleNotificationDateTime.second,
millisecond: scheduleNotificationDateTime.millisecond
)
);
}
Future cancelScheduledNotification(int id) async {
await AwesomeNotifications().cancel(id);
print("$id cancelled scheduled notification.");
}
static Future startListeningNotificationEvents() async {
AwesomeNotifications().setListeners(onActionReceivedMethod: onActionReceivedMethod);
}
static Future onActionReceivedMethod(
ReceivedAction receivedAction
) async {
if (receivedAction.buttonKeyPressed == 'done') {
final db = RemindersData();
db.deleteReminder(receivedAction.id ?? 7);
// refreshHomePageCallback!();
}
else
{
print("Unknown action with notification.");
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/780 ... tion-is-cl
Мобильная версия