Remove/Cancel Local Notifications

Hello everyone, I’m creating my first app and I created a Local notification with UNUserNotificationCenter but I’m not figuring how to cancel if the user cancel the action, can someone give me a tip?

thanks in advance.

Take a look at this example code from Paul Hudson which I have modified to store an array of requestIdentifiers

struct ContentView: View {
    @State var requestIdentifiers = [String]()

    var body: some View {
        VStack(spacing: 50) {
            Button("Request Permission") {
                UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
                    if success {
                        print("All set!")
                    } else if let error = error {

            Button("Schedule Notification") {
                let content = UNMutableNotificationContent()
                content.title = "Feed the cat"
                content.subtitle = "It looks hungry"
                content.sound = UNNotificationSound.default

                // show this notification five seconds from now
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)

                // choose a random identifier
                let identifier = UUID().uuidString

                // Add that to the array

                // Create the request using the identifier above
                let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

                // add our notification request

            Button("Remove Notifications") {
                UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: requestIdentifiers)

Place the above code in a new project and test it on your simulator.
Tap Request Permission then Schedule Notification and then lock the device using the keyboard shortcut Command + L
The notification should pop up in 5 seconds.
Open the App and Schedule Notification then tap Remove Notifications and lock the device Command + L.

Note that you will not receive a notification which shows that the notification was removed.

1 Like

Thank you Chris, I’ll try that. Appreciated