【问题标题】:Error APNS device token not set before retrieving FCM Token for Sender ID在检索发件人 ID 的 FCM 令牌之前未设置错误 APNS 设备令牌
【发布时间】:2021-11-27 18:39:17
【问题描述】:

我正在接收来自 firebase 的消息,以获取带有 APN 的通知。在 Firebase 中,我有 APNs 密钥的证书,在 Firebase 中的 Xcode 项目中具有相同的 ID,该 ID 是从 Apple Developer 中提取的。

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

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

在检索发件人 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)")
     }
}

【问题讨论】:

    标签: ios firebase push-notification firebase-cloud-messaging access-token


    【解决方案1】:

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

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

    【讨论】:

    • 是的,我在尝试订阅主题时(在真实设备上)仍然遇到同样的错误
    【解决方案2】:

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

    这可能是由几个主要原因造成的。

    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 捆绑包标识符。
    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 有 3 个酒吧中的 2 个,但在使用与我的开发笔记本电脑相同的 wifi 时没有连接到互联网。我的笔记本电脑没有问题,所以我认为我的手机连接也正常。超过一天被浪费了,因为我假设。因此,请仔细检查您手机的连接是否正常。

    更多信息: 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。

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

    【讨论】:

      【解决方案3】:

      确保不要重命名授权密钥文件

      就我而言,我已将身份验证密钥文件重命名为其他内容,这导致了问题。我尝试将其命名回默认格式AuthKey_<KeyID>.p8,一切都开始工作了。

      【讨论】:

        猜你喜欢
        • 2020-02-03
        • 1970-01-01
        • 2021-06-28
        • 2022-11-02
        • 2020-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多