У меня есть несколько уведомлений по расписанию в моем приложении, когда я хочу отменить уведомления, которые уже зарегистрированы по времени, локальные уведомления не могут быть отменены. Я использовал flutter_local_notifications: ^ 0.8.4 + 3 library
Вот мой код
if (_monthlyList[i].datetime.awal.isAfter(_now)) {
await _monthlyNotification(
uniqueIdAwal(i), _monthlyList[i].datetime.awal, "awal");
await _monthlyNotification(
uniqueIdTengah(i), _monthlyList[i].datetime.tengah, "tengah");
await _monthlyNotification(
uniqueIdAkhir(i), _monthlyList[i].datetime.akhir, "akhir");
}
if (_monthlyList[i].datetime.awal.isBefore(_now)){
print('id cancel date awal '+uniqueIdAwal(i).toString());
await _cancelNotification(uniqueIdAwal(i));
}
if (_monthlyList[i].datetime.tengah.isBefore(_now)){
print('id cancel date tangah '+uniqueIdTengah(i).toString());
await _cancelNotification(uniqueIdTengah(i));
}
if (_monthlyList[i].datetime.akhir.isBefore(_now)){
print('id cancel date akhir'+uniqueIdAkhir(i).toString());
await _cancelNotification(uniqueIdAkhir(i));
}}
Это для отмены
Future<void> _cancelNotification(int _id) async {
await flutterLocalNotificationsPlugin.cancel(_id);}
Это для уведомления
void _initMonthlyNotif() async {
if (await _notifPreferences.getAyyumulBidh()) {
DateTime _now = DateTime.now();
for (var i = 0; i < _monthlyList.length; i++) {
if (_monthlyList[i].datetime.awal.isAfter(_now)) {
await _monthlyNotification(
uniqueIdAwal(i), _monthlyList[i].datetime.awal, "awal");
await _monthlyNotification(
uniqueIdTengah(i), _monthlyList[i].datetime.tengah, "tengah");
await _monthlyNotification(
uniqueIdAkhir(i), _monthlyList[i].datetime.akhir, "akhir");
}
if (_monthlyList[i].datetime.awal.isBefore(_now)){
await _cancelNotification(uniqueIdAwal(i));
}
if (_monthlyList[i].datetime.tengah.isBefore(_now)){
await _cancelNotification(uniqueIdTengah(i));
}
if (_monthlyList[i].datetime.akhir.isBefore(_now)){
print('id cancel date akhir'+uniqueIdAkhir(i).toString());
await _cancelNotification(uniqueIdAkhir(i));
}
}
} else {
for (var i = 0; i < _monthlyList.length; i++) {
await this._cancelNotification(uniqueIdAwal(i));
await this._cancelNotification(uniqueIdTengah(i));
await this._cancelNotification(uniqueIdAkhir(i));
}
}}