Why is willPresentNotification called twice here?

Please find below a complete app example.

It has a button, when you press it, a local notification is created. However, the UnNotificationCenter.delegate is called twice, and I can't understand why.

I am trying to move my project from Objective-C to Swift, and my similar code there doesn't get called twice, so I'm confused.

Can anybody shine a light on this? Pointers appreciated.

App:


@main
struct NotifTestApp: App {
    init() {
        UNUserNotificationCenter.current().delegate = NotificationReceiveHandler.shared
        configureUserNotifications()
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
    
private func configureUserNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
            if granted {
                print("Notification permission granted.")
            } else if let error = error {
                print("Error requesting notification permissions: \(error)")
            }
        }
    }

}

class NotificationReceiveHandler: NSObject, UNUserNotificationCenterDelegate {
    static let shared = NotificationReceiveHandler()

//>> THIS IS CALLED TWICE WHEN I PRESS THE BUTTON
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        NSLog(">>> Will present notification!")
        completionHandler([.sound])
    }
}

///THE UI
struct ContentView: View {
    var body: some View {
        VStack {
            Text("👾")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Notification test!")
            Text("When i press the button, will present is called twice!!").font(.footnote)
                .padding(10)
            Button("Create Notification") {
                createNotification(
                    message: "This is a test notification",
                    header: "Test Notification",
                    category: "TEST_CATEGORY",
                    playSound: true,
                    dictionary: nil,
                    imageName: nil)
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

private func createNotification(message: String, header: String, category: String, playSound: Bool = true, dictionary: NSDictionary? = nil, imageName: String? = nil) {
    let content = UNMutableNotificationContent()
    content.title = header
    content.body = message
    content.categoryIdentifier = category
    content.badge = NSNumber(value: 0)

    if let imageName = imageName, let imageURL = Bundle.main.url(forResource: imageName, withExtension: "png") {
        do {
            let attachment = try UNNotificationAttachment(identifier: "image", url: imageURL, options: nil)
            content.attachments = [attachment]
        } catch {
            print("Error creating notification attachment: \(error)")
        }
    }

    content.sound = playSound ? UNNotificationSound(named: UNNotificationSoundName("event.aiff")) : nil
    if let infoDict = dictionary {
        content.userInfo = infoDict as! [AnyHashable: Any]
    }

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
Answered by Engineer in 800720022

If you are seeing this on iOS 18 betas, this is due to a known issue currently being investigated.


Argun Tekant /  DTS Engineer / Core Technologies

Accepted Answer

If you are seeing this on iOS 18 betas, this is due to a known issue currently being investigated.


Argun Tekant /  DTS Engineer / Core Technologies

Hey Argun, thanks for responding. I'm using it on an iPhone running the beta, yes, but not beta Xcode. Would that be affected?

EDIT: Thanks, I tried myself and can verify that it's a bug on ios18. Tried on my wife's phone and it's only called once there.

Just checked beta 7 that was released just now, it wasn't fixed there.

..and in case anyone is looking at this, it's not fixed in beta 8 either.

Same issue happens on public Xcode Version 16.0 (16A242d) and iOS 18.0

I’m experiencing the same issue. Did you find a solution?

same issue with iOS 18.0 (22A3354) using xcode version 16.0 (16A242d).

Still getting this issue.

Running Xcode 16.1 (16B40) and everything is fixed now.

Why is willPresentNotification called twice here?
 
 
Q