添加Notification Service Extension后iOS出现firebase_auth/notification-not-forwarded错误
iOS添加Notification Service Extension后Firebase Phone Auth验证码验证失效解决方案
问题原因
添加Notification Service Extension后,iOS远程通知会优先经过Extension处理,可能导致主App的AppDelegate无法接收到Firebase Phone Auth的验证码通知;若手动禁用了Firebase的方法swizzling,未将通知转发给FIRAuth也会触发该错误。
解决方案
情况1:默认开启Swizzling(Firebase默认配置)
Notification Service Extension会拦截所有远程通知,需让它跳过Firebase Auth的验证码通知,交给主App处理:
- 在Extension的
NotificationService.swift中,判断通知的userInfo是否包含Firebase相关字段,若是则直接调用contentHandler,不做自定义处理:
import UserNotifications class NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? var bestAttemptContent: UNMutableNotificationContent? override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) // 识别Firebase Auth通知,直接转交主App if let userInfo = request.content.userInfo as? [String: Any], userInfo["firebase"] != nil { contentHandler(request.content) return } // 处理你的自定义通知逻辑(如添加附件、修改内容) if let bestAttemptContent = bestAttemptContent { contentHandler(bestAttemptContent) } } override func serviceExtensionTimeWillExpire() { if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { contentHandler(bestAttemptContent) } } }
情况2:手动禁用了Swizzling
若你在Info.plist中添加了FirebaseAppDelegateProxyEnabled并设为NO,需在主App的AppDelegate.swift中手动转发通知给Firebase Auth:
import Firebase import FirebaseAuth @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { FirebaseApp.configure() return true } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // 转发Auth通知给Firebase处理 if Auth.auth().canHandleNotification(userInfo) { completionHandler(.noData) return } // 处理其他类型通知 completionHandler(.noData) } }
额外检查项
- 确保主App和Extension的Bundle ID配置正确(Extension需为主App Bundle ID加后缀,如
com.yourapp.notification-service) - 确认Apple开发者账号中已为Extension开启Push Notification权限,且APNs证书配置正确
- 更新Firebase SDK到最新版本,避免版本兼容问题
内容的提问来源于stack exchange,提问作者Thanon K
相关产品推荐
相关产品推荐

