Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iamcalledrob/74655b3313262ebca50efbc7dbe0520d to your computer and use it in GitHub Desktop.
Save iamcalledrob/74655b3313262ebca50efbc7dbe0520d to your computer and use it in GitHub Desktop.
Fix for UNUserNotificationCenter.removeDeliveredNotifications not actually removing notifications
extension UNUserNotificationCenter {
func removeDeliveredNotificationsAsync(withIdentifiers identifiers: [String]) async {
// Calls to `UNUserNotificationCenter.current().removeDeliveredNotifications` schedule work
// on a background thread, with no completion handler or async/await interface. There is no
// way to know whether the work completed or not. If the notification service extension exits
// before the work has been completed, the work is cancelled and notifications are not removed.
//
// This is really poor API design on Apple's part, and even poorer documentation
//
// https://stackoverflow.com/questions/54565979#comment104110958_54680225
removeDeliveredNotifications(withIdentifiers: identifiers)
// It appears that deliveredNotifications waits for the background work to complete before
// returning itself (tested on iOS 16). If this proves to be unreliable, then a timer (sigh)
// or iteration over deliveredNotifications will also work.
await deliveredNotifications()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment