Ja pracujem na App, ktorý mi dávaupozornenie na konkrétny čas a dátum a zobrazí sa v TableViewController. Takže mám viac oznámení v tabuľke. Ako odstránim konkrétne upozornenie? Ak odstránim riadok obsahujúci oznámenie, stále sa nevymaže.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let context = getContext()
if editingStyle == .delete {
// Delete the row from the data source
context.delete(names[(indexPath as NSIndexPath).row])
let a = (names[(indexPath as NSIndexPath).row])
let center = UNUserNotificationCenter.current()
let notifToDelete = a.name
center.getPendingNotificationRequests { (notifications) in
print(notifications)
for item in notifications {
if(item.identifier.contains(notifToDelete!))
{
center.removePendingNotificationRequests(withIdentifiers: [item.identifier])
}
}
}
do {
try context.save ()
}catch {
let saveError = error as NSError
print(saveError)
}
names.remove(at: (indexPath as NSIndexPath).row)
tableView.deleteRows(at: [indexPath], with: .automatic)
To žiadnym spôsobom neovplyvňuje. Oznámenie stále beží v nastavenom čase. (Notifikácia je nastavená v inom View Controlleri).
odpovede:
3 pre odpoveď č. 1Odstráňte tento blok nepotrebného kódu:
center.getPendingNotificationRequests { (notifications) in
print(notifications)
for item in notifications {
if(item.identifier.contains(notifToDelete!))
{
center.removePendingNotificationRequests(withIdentifiers: [item.identifier])
}
}
}
Namiesto toho postupujte takto:
center.removePendingNotificationRequests(withIdentifiers: [notifToDelete])