0

I set up a dummy project to reproduce the issue I'm seeing. In my ContentView, I schedule some repeating notifications.

struct ContentView: View {
    var body: some View {
        VStack {
            Button("Schedule notifications") {
                let content = UNMutableNotificationContent()
                content.title = "Title"
                content.body = "body"
                content.sound = UNNotificationSound.default
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
                let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
                UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
            }

            Button("Request Permission") {
                let current = UNUserNotificationCenter.current()
                current.requestAuthorization(
                    options: [.sound, .alert],
                      completionHandler: { completed, wrappedError in
                          guard let error = wrappedError else {
                              return
                          }
                  })
            }
        }

    }
}

Then in my AppDelegate, I attempt to cancel those repeating notifications before the app terminates.

   func applicationWillTerminate(_ application: UIApplication) {
        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        print("============== removing all notifications")
    }

What I'm finding is that my scheduled notifications are still delivered, even though I can see my print statement in the Xcode console. But if I run the same test on an iPhone, my notification is not delivered, as expected.

Am I doing something wrong, or is this a bug? I'm using 13.4.1 on iPad, and 13.3.1 on iOS

  • just for the sake of narrowing down where the problem is coming from. Try removing all notifications from outside `applicationWillTerminate` and see if it works. If it still doesn't work then you can remove the discussion about `applicationWillTerminate`... – mfaani Jan 01 '21 at 13:36

1 Answers1

0

I am also facing the same problem. Found out that applicationWillTerminate method has approximately five seconds to perform any tasks and return. And removeAllPendingNotificationRequests returns immediatly but removes scheduled notification asynchronously in secondary thread.

I think 5 seconds are more than necessary to clear notification.