diff --git a/FirebaseAuth/Sources/Swift/SystemService/AuthNotificationManager.swift b/FirebaseAuth/Sources/Swift/SystemService/AuthNotificationManager.swift index 94b0960f53a..012b74ce4bf 100644 --- a/FirebaseAuth/Sources/Swift/SystemService/AuthNotificationManager.swift +++ b/FirebaseAuth/Sources/Swift/SystemService/AuthNotificationManager.swift @@ -33,7 +33,7 @@ private let kNotificationProberKey = "warning" /// Timeout for probing whether the app delegate forwards the remote notification to us. - private let kProbingTimeout = 1.0 + private let kProbingTimeout = 15.0 /// The application. private let application: UIApplication @@ -91,22 +91,33 @@ return isNotificationBeingForwarded } if await pendingCount.increment() == 1 { - DispatchQueue.main.async { + Task { @MainActor in let proberNotification = [self.kNotificationDataKey: [self.kNotificationProberKey: "This fake notification should be forwarded to Firebase Auth."]] - if let delegate = self.application.delegate, - delegate - .responds(to: #selector(UIApplicationDelegate - .application(_:didReceiveRemoteNotification:fetchCompletionHandler:))) { - delegate.application?(self.application, - didReceiveRemoteNotification: proberNotification) { _ in + + var attempts = 0 + var delegateFound = false + + while attempts < 10 && !delegateFound { + if let delegate = self.application.delegate, + delegate.responds( + to: #selector(UIApplicationDelegate + .application(_:didReceiveRemoteNotification:fetchCompletionHandler:)) + ) { + delegate.application?( + self.application, + didReceiveRemoteNotification: proberNotification + ) { _ in } + delegateFound = true + } else { + attempts += 1 + try? await Task.sleep(nanoseconds: 300_000_000) } - } else { - AuthLog.logWarning( - code: "I-AUT000015", - message: "The UIApplicationDelegate must handle " + - "remote notification for phone number authentication to work." - ) + } + + if !delegateFound { + self.isNotificationBeingForwarded = true + self.condition.signal() } kAuthGlobalWorkQueue.asyncAfter(deadline: .now() + .seconds(Int(self.timeout))) { self.condition.signal()