HI I am working on an App that gives me notification for a specific time and date and is displayed in a TableViewController. So i have multiple notifications in the table . How do i delete a particular notification ? If i delete the row containing the notification it still does not get deleted .
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)
This does not affect in any way . The notification still runs at the set time. (The notification is set in a different View Controller ) .
Remove this block of unnecessary code:
center.getPendingNotificationRequests { (notifications) in
print(notifications)
for item in notifications {
if(item.identifier.contains(notifToDelete!))
{
center.removePendingNotificationRequests(withIdentifiers: [item.identifier])
}
}
}
Instead, do this:
center.removePendingNotificationRequests(withIdentifiers: [notifToDelete])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With