skip to Main Content

I am using local notifications and storing the data in core data. I am using the editingStyle .delete to delete the reminder with a swipe to the left action. Figured out how to delete the data with context.delete but the reminder still displays since I have not removed the pending notification when swipe delete. I have used UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
and as figured after just deleting just one reminder all the others even though not deleted from tableView or core data the notification doesn’t show as expected.

When creating my notification I am using "UUID().uuidString" as my identifier for my UNNotificationRequest to create a unique identifier every time the user created a new reminder. My question is how do I remove a pending notification when using unique identifier with UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: )

Here is my code when creating the notification and when deleting it:

@IBAction func dateReminder(){
    
    guard let dateVc = storyboard?.instantiateViewController(withIdentifier: "dateVc") as? DateViewController else {
        return
    }
    dateVc.completion = {title,body,date in
        DispatchQueue.main.async {
            self.navigationController?.popToRootViewController(animated: true)
            let new = Reminder(context: self.context)
            new.title = title
            new.body = body
            new.detailsDate = date
            new.detailsBool = false
            
            self.reminder.append(new)
            self.saveData()
            
            
            let content = UNMutableNotificationContent()
            content.title = title
            content.body = body
            content.sound = .default
            
            let dateSelected = date
            let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.year, .month, .day, .hour , .minute , .second], from: dateSelected), repeats: false)
            let uuidString = UUID().uuidString
            let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)
            UNUserNotificationCenter.current().add(request) { error in
                if let e = error{
                    print("An error occurred. (e)")
                }
            }
            
        }
        
    }
    
    navigationController?.pushViewController(dateVc, animated: true)
    
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete{
        tableView.beginUpdates()
        
        context.delete(reminder[indexPath.row])
        reminder.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
        UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: )
        tableView.endUpdates()
        self.saveData()
        
    }
}

2

Answers


  1. To Delete Reminders Using Identifiers I Use Following Code…

    func cancelReminderWithIdentifier(identifier : String) {
        UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
           var identifiers: [String] = []
           for notification:UNNotificationRequest in notificationRequests {
               if notification.identifier == identifier {
                  identifiers.append(notification.identifier)
               }
           }
            self.UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
        }
    }
    

    Pass identifier in function and You will have your Notification Delete.

    For Saving Your UUID,

        UNUserNotificationCenter.current().add(request) { error in
                if let e = error{
                    print("An error occurred. (e)")
                } else {
                    //You Can Save Your UUID and Other Data Here
                    //Just Example
                    CoreDataOperation.addNewModel(UUIDString : uuidString, title : title)
                }
            }
    

    Note : – You can find Other Threads useful for Using Core Data.

    Login or Signup to reply.
  2. If you want to delete all pending notifications requests, call this function

    @objc func cancelNotifications() -> Void {
        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search