I ended up doing something like this:
extension AppDelegate: UNUserNotificationCenterDelegate {
/// This is called when a notification is received while the app is in the foreground.
/// There is an iOS 18 bug where it is called twice (https://forums.developer.apple.com/forums/thread/762126).
/// As a workaround we are checking for duplicate ids and ignoring the second notification
func userNotificationCenter(
_: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
let lastNotificationKey = "lastNotificationKey"
var isDuplicateNotification = false
if let lastNotification = UserDefaults.standard.value(forKey: lastNotificationKey) as? String {
isDuplicateNotification = lastNotification == notification.request.identifier
}
UserDefaults.standard.set(notification.request.identifier, forKey: lastNotificationKey)
// If this is not a duplicate notification, process it normally
if !isDuplicateNotification {
// Add code to process notification using notification.request.content.userInfo
}
completionHandler([])
}
}