You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

集成APNs后iOS应用出现Firebase Phone Auth‘无效令牌’错误

iOS集成APNs后Firebase电话认证报'invalid token'问题解决

问题场景

原本使用Firebase电话认证正常的iOS应用,在集成APNs(后端直接推送,不通过Firebase)并在Xcode启用APNs后,调用verifyPhoneNumber时触发'invalid token'错误。

相关代码

登录认证代码

let verificationID = UserDefaults.standard.string(forKey: "authVerificationID")
    
PhoneAuthProvider.provider()
    .verifyPhoneNumber(self.phone_no, uiDelegate: nil) { verificationID, error in
        if let error = error {
            print("-----------PHONE AUTH:",error.localizedDescription) // 此处触发invalid token错误
            return
        }
        UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
    }

AppDelegate推送相关代码

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { success, _ in
        guard success else {
            return
        }
        print("Success in APNS registry")
    }
    
    application.registerForRemoteNotifications()
    
    UIApplication.shared.isIdleTimerDisabled = true
    
    return true;
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenParts = deviceToken.map { data -> String in
        return String(format: "%02.2hhx", data)
    }
    let token = tokenParts.joined()
    UserInfo.apnToken = token
    print("Messaging Delegate Token: \(token)")
    print("Messaging Delegate Token UserInfo: \(UserInfo.apnToken)")
    //NotificationUtils.updateDeviceToken();
}

解决方法

1. 确保Firebase Messaging正常初始化

Firebase电话认证依赖Firebase Messaging的Instance ID token,即使不用Firebase推送,也需要保证该组件正常获取token。修改AppDelegate:

  • 导入FirebaseMessaging
  • 设置Messaging代理并实现token回调

修改后的关键代码:

import FirebaseMessaging

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        
        // 绑定Firebase Messaging代理
        Messaging.messaging().delegate = self
        
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { success, _ in
            guard success else {
                return
            }
            print("Success in APNS registry")
        }
        
        application.registerForRemoteNotifications()
        
        UIApplication.shared.isIdleTimerDisabled = true
        
        return true;
    }
    
    // 新增Firebase Messaging token回调
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        print("FCM Token obtained: \(fcmToken ?? "nil")")
        // 无需传递给自定义后端,确保Firebase能获取到该token即可
    }
    
    // 保留原有APNs token处理逻辑
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }
        let token = tokenParts.joined()
        UserInfo.apnToken = token
        print("APNs Token: \(token)")
        print("APNs Token saved: \(UserInfo.apnToken)")
        //NotificationUtils.updateDeviceToken();
    }
}

2. 检查Xcode Capabilities配置

  • 确认Push Notifications已开启(Target -> Signing & Capabilities)
  • 确认Background Modes中勾选了Remote notifications,Firebase电话认证依赖该通道完成验证码下发

3. 清理缓存并重装应用

  • 卸载设备上的应用
  • 清空Xcode的Derived Data(Xcode -> Settings -> Locations -> Derived Data -> Delete)
  • 重新编译运行

4. 验证Firebase控制台配置

  • 确认Firebase项目中iOS应用的APNs证书有效(即使使用自定义后端推送,Firebase仍需该证书验证设备合法性)
  • 确认Xcode中的Bundle ID与Firebase控制台注册的应用Bundle ID完全一致

原因分析

集成APNs后,Firebase Messaging未被正确初始化,导致无法获取有效的Instance ID token,而Firebase电话认证需要该token验证请求合法性,因此触发'invalid token'错误。即使不使用Firebase推送服务,也需保证Firebase Messaging组件正常运行。

内容的提问来源于stack exchange,提问作者RAS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 12:58:16