在检索发件人 ID 的 FCM 令牌之前未设置错误 APNS 设备令牌

Error APNS device token not set before retrieving FCM Token for Sender ID

我正在接收来自 firebase 的关于 APN 通知的消息。在 firebase 中,我有 APNs 密钥的证书,在从 Apple Developer 中提取的 Xcode 项目中具有相同的 id。

但我不知道为什么会发生这种情况,我得到了这个错误,它在消息扩展中注册了两个令牌:

extension AppDelegate : MessagingDelegate {
  func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {}}

APNS device token not set before retrieving FCM Token for Sender ID '########'. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set.

添加了我在 AppDelegate 中的内容

import Firebase
import MasivPushIosSdk

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{

    var firebaseToken: String = ""
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        FirebaseApp.configure()
        self.registerForFirebaseNotification(application: application)
        Messaging.messaging().delegate = self
        return true
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
    }

    func registerForFirebaseNotification(application: UIApplication) {
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()
    }
    
}

extension AppDelegate: MessagingDelegate, UNUserNotificationCenterDelegate {

//MessagingDelegate
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        self.firebaseToken = fcmToken!
        print("Firebase token: \(fcmToken)")
    }

    //UNUserNotificationCenterDelegate
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("APNs received with: \(userInfo)")
     }
}

这是一个模拟器日志。您可以安全地忽略它。你得到这个的原因是 Firebase 试图创建一个从 FCM 令牌到 APNS 令牌的映射,这样它就可以将 APNS 消息发送到 iOS 设备。但是,模拟器上没有APNS令牌,所以映射失败。

尝试在实际设备上进行测试,看看是否仍然出现错误。

我在这个问题上花了很长时间。我终于可以使用远程消息了。

主要有以下几个原因。

  1. GoogleService-Info.plist 未通过 xcode 添加到您的项目中(如果需要,勾选复制文件)
  2. GoogleService-Info.plist 应该出现在 Build Phases -> Copy Bundle Resources 中。如果1完成,它会自动在这里。
  3. (如果您使用的是 React Native)请确保您的 GoogleService-Info.plist 信息指向与您的 Javascript 代码相同的 Firebase 项目。
  4. Apple 服务密钥和使用此密钥的 firebase 项目应指向相同的 apple bundle 标识符。
  5. (如果您使用的是 React Native)将其放入 componentDidMount()
const authStatus = await firebase.messaging().requestPermission();
  if (authStatus === 1) {
    let fcmToken = await firebase.messaging().getToken();
    if (fcmToken) {
      console.log('Firebase Push Token is:', fcmToken);
    } 
  }
  1. 最后确保您的设备可以正常连接到互联网。我被困在这里,我的 iPhone X 有 2 个,共 3 个,但在使用与我的开发笔记本电脑相同的 wifi 时没有连接到互联网。我的笔记本电脑没有问题,所以我认为我的 phone 连接也正常。因为我假设,所以浪费了一天多的时间。所以,只需仔细检查您的 phone 的连接是否正常。

更多信息: A)如果本地工作:https://firebase.google.com/docs/cloud-messaging/ios/client B) 如果使用 React Native:https://rnfirebase.io/messaging/usage/ios-setup

注意:请勿混用 A 和 B。一个用于原生 IOS,另一个用于 React Native。

p.s。我添加了 React native,因为我在使用它时在搜索中遇到了你的问题。

确保您没有重命名授权密钥文件

就我而言,我已将授权密钥文件重命名为其他内容,这就是导致问题的原因。我尝试将其命名回默认格式 AuthKey_<KeyID>.p8 并且一切都开始工作了。

在我们的案例中,我们注意到上面有一条日志:

项目的Bundle ID与'GoogleService-Info.plist'

中的Bundle ID不一致

因此,情况是:我们应用的 捆绑包标识符 不同于我们为 Firebase 项目创建 iOS 应用时使用的标识符。

使它们相同,问题解决了。