12.3.0 - [GoogleUtilities/AppDelegateSwizzler][I-SWZ001014] App Delegate не соответствует протоколу UIApplicationDelegate.
Токен получен, но оказывается равным нулю.
Код: Выделить всё
import SwiftUI
import Firebase
import FirebaseAuth
import FirebaseCore
import UserNotifications
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
// ✅ Store the token until we can safely set it
private var pendingAPNsToken: Data?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
// Set up notification delegate
UNUserNotificationCenter.current().delegate = self
// Request notification authorization for MFA
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if granted {
print("✅ Notification permission granted")
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
} else if let error = error {
print("❌ Notification permission error: \(error.localizedDescription)")
}
}
print("✅ AppDelegate setup complete")
return true
}
// ✅ This receives the APNs token from iOS
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
print("📱 APNs token received: \(tokenString)")
// Store token and attempt to set it after a delay
self.pendingAPNsToken = deviceToken
// Wait a bit longer to ensure Auth is fully initialized
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
self?.setAPNsToken()
}
}
// ✅ Set the APNs token once Auth is ready
private func setAPNsToken() {
guard let token = pendingAPNsToken else { return }
Auth.auth().setAPNSToken(token, type: .unknown)
print("✅ APNs token set in Firebase Auth")
pendingAPNsToken = nil
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("❌ Failed to register for remote notifications: \(error.localizedDescription)")
}
// ✅ Handle incoming silent push (verification SMS from Firebase)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping(UIBackgroundFetchResult) -> Void) {
print("📩 Remote notification received: \(userInfo)")
if Auth.auth().canHandleNotification(userInfo) {
print("✅ Firebase Auth handled the notification (MFA verification)")
completionHandler(.noData)
return
}
print("ℹ️ Notification not handled by Firebase Auth")
completionHandler(.noData)
}
// ✅ Handle notifications when app is in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("📩 Foreground notification received")
// For MFA silent notifications, don't show UI
completionHandler([])
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... pping-an-o
Мобильная версия