Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete notifications in swift 3

Tags:

ios

swift

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 ) .

like image 806
ASJ_14 Avatar asked Dec 18 '25 23:12

ASJ_14


1 Answers

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])
like image 193
The Mach System Avatar answered Dec 20 '25 16:12

The Mach System



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!