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
To Delete Reminders Using Identifiers I Use Following Code…
Pass identifier in function and You will have your Notification Delete.
For Saving Your UUID,
Note : – You can find Other Threads useful for Using Core Data.
If you want to delete all pending notifications requests, call this function