Best practice for removePendingNotificationRequests with CoreData

Hello all,

I have a question regarding how to use the removePendingNotificationRequests when implemented in CoreData.

Currently, I have an app that lets users save meeting URLs. Each meeting the user creates is stored as a CoreData entity. They can set a time for these meetings, and the app will schedule a local notification for a few minutes before the meeting. The way I'm doing this is passing the meeting time the user selected into a sendNotification function and using that time as a trigger (UNCalendarNotificationTrigger).

Problem is -- once a user creates a meeting and the notification is set, the notification will happen even if the user deletes the meeting before the scheduled time. I want to use removePendingNotificationRequests to solve this problem. My question is: removePendingNotificationRequests accepts a string as the identifier for which notification request to remove. But, how do I pass this string to the request? And which string should I use? The meeting's name? I've been thinking about creating a new UUID attribute for my meeting entity so I'll always have a unique string, and passing that UUID to the onDelete function of the meetings.

Here's how I'm passing the meeting time to the notification function (this is inside a "Create" button in a AddNewMeeting view):

Code Block
self.notificationManager.sendNotification(title: "Upcoming Meeting", subtitle: nil, body: "\(meetingName) will start in 5 minutes. Tap here to get meeting information.", time: meetingTime, meetingName: "\(meetingName)")


This is my current code for the list of meetings:

Code Block
ForEach(items, id: \.self) { item in
       OneTimeRow(meeting: item)
}
.onDelete(perform: deleteItems)


And this is the delete function:

Code Block
    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            offsets.map { items[$0] }.forEach(viewContext.delete)
            do {
                try viewContext.save()
            } catch {
                print("There was an error deleting items")
            }
        }
removePendingNotificationRequests(withIdentifiers identifiers: [String]) // <-- Is this how to put the function in?
    }

And this is the code for scheduling notifications:

Code Block
    func sendNotification(title: String, subtitle: String?, body: String, time: Date, meetingName: String) {
            let content = UNMutableNotificationContent()
            content.title = title
            if let subtitle = subtitle {
                content.subtitle = subtitle
            }
            content.body = body
            let triggerDate = time - 5 * 60
                let trigger = UNCalendarNotificationTrigger(
                            dateMatching: Calendar.current.dateComponents([.timeZone, .year, .month, .day, .hour, .minute], from: triggerDate),
                            repeats: true
                )
        let request = UNNotificationRequest(identifier: "\(meetingName)", content: content, trigger: trigger)
                UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        }


Thank you!
Answered by Claude31 in 663611022
That is what you should use.

Of course, pass the actual Strings, with the exact meeting identifier
Code Block
removePendingNotificationRequests(withIdentifiers identifiers: [meetingID])


Note also :

This method executes asynchronously, removing the pending notification requests on a secondary thread.


Oh, you want to run it. Let me give you the complete source code. Here
Thanks. But you sent only the .xcodeproj file, without any content file.
You have to link to a zip of the complete project folder.
Best practice for removePendingNotificationRequests with CoreData
 
 
Q