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

dav*_*as 16 push-notification access-token ios firebase firebase-cloud-messaging

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

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

extension AppDelegate : MessagingDelegate {
  func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {}}
Run Code Online (Sandbox Code Playgroud)

在检索发件人 ID“########”的 FCM 令牌之前未设置 APNS 设备令牌。对此 FCM 令牌的通知将不会通过 APNS 传递。设置 APNS 设备令牌后,请务必重新检索 FCM 令牌。

添加了我在 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)")
     }
}
Run Code Online (Sandbox Code Playgroud)

bab*_*ibo 6

这是仅模拟器的日志。您可以安全地忽略它。您收到此消息的原因是 Firebase 尝试创建从 FCM 令牌到 APNS 令牌的映射,以便它可以将 APNS 消息发送到 iOS 设备。但是,模拟器上没有 APNS 令牌,因此映射失败。

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

  • 是的,在尝试订阅主题(在真实设备上)时我仍然遇到相同的错误 (21认同)
  • 此信息不正确。通知适用于 XCode 提供的模拟器和物理设备。如果您收到此消息,则意味着消息传递框架尚未通过 AppDelegate.swift 中的 Messaging.messaging().apnsToken = deviceToken 提供 APNS 令牌。有关更多详细信息以及完整的代码和参考,请参阅我下面的帖子。 (3认同)
  • 我也有此日志,但推送通知在我的设备上运行良好。在设备上忽略它也可以吗? (2认同)

Dav*_*vid 5

我在这个问题上花了很长时间。我终于让它与远程消息一起工作了。

它可能是由几个主要原因引起的。

  1. GoogleService-Info.plist 未通过 xcode 添加到您的项目(如果需要,请选中复制文件)
  2. GoogleService-Info.plist 应显示在“构建阶段”->“复制捆绑资源”中。如果 1 完成,它会自动出现在这里。
  3. (如果您使用的是 React Native)请确保您的 GoogleService-Info.plist 信息指向您的 Javascript 代码所在的同一个 Firebase 项目。
  4. Apple 服务密钥和使用此密钥的 firebase 项目应指向相同的 Apple 捆绑包标识符。
  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);
    } 
  }
Run Code Online (Sandbox Code Playgroud)
  1. 最后确保您的设备可以正常连接到互联网。我被困在这里,我的 iPhone X 有 3 个格中的 2 个,但在使用与我的开发笔记本电脑相同的 wifi 时无法连接到互联网。我的笔记本电脑没有问题,所以我认为我的手机连接也正常。因为我假设,浪费了一天多的时间。因此,只需仔细检查手机的连接是否正常即可。

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

注意:不要混合使用 A 和 B。一个适用于原生 IOS,另一个适用于 React Native。

ps 我添加了 React Native ,因为我在使用它时在搜索中遇到了你的问题。


小智 5

这可能会迟到,但是错误

在检索发件人 ID“########”的 FCM 令牌之前未设置 APNS 设备令牌。对此 FCM 令牌的通知将不会通过 APNS 传递。设置 APNS 设备令牌后,请务必重新检索 FCM 令牌。

通过添加此内容为我排序

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("Yay! Got a device token  \(deviceToken)")
        Messaging.messaging().setAPNSToken(deviceToken, type: .unknown)
    } 
Run Code Online (Sandbox Code Playgroud)