iOS 18 when app in foreground the notiification will prensent twice

Hi. all

Now we test our app in iOS18. When app in foreground mode, the app will present our alert controller twice.We used the follow test(send local notification): UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in if error == nil { print("") } })

also have same issue.

Debug code, we found the below method have been called by system twice.

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(macos(10.14), ios(10.0), watchos(3.0), tvos(10.0));

Anyone have the same issue? Do you have any idea? Thank you .

This is currently a known issue in iOS 18.

If this issue is critical for your apps, in the meantime while a solution is pending, you can use UNNotificationRequest.identifier or UNNotificationContent.threadIdentifier for local notifications or the thread-id key in the push notification payload (or any other notification related data) to try to recognize the two back to back callbacks may have been for the one single notification.

Unfortunately while the issue is under investigation we are not able to say when a resolution might be available.


Argun Tekant /  DTS Engineer / Core Technologies

Same issue

some fix for this?

We're suffering the same issue. Is there anything we can do as a workaround to prevent duplicate notifications?

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([])
    }
}

The bug seems to be solved with iOS 18.1. Can anyone else confirm?

Yes, the issue was solved for me

iOS 18 when app in foreground the notiification will prensent twice
 
 
Q