【问题标题】:Swift 4, Firebase 5.8.0 FCM token nilSwift 4、Firebase 5.8.0 FCM 令牌无
【发布时间】:2023-04-03 04:18:01
【问题描述】:

我正在设置推送通知,一切都很顺利,直到我尝试获取 FCM 令牌,以便我可以向实际设备发送测试消息。使用 Pod Firebase 5.8.0、FirebaseCore (5.1.3)、FirebaseInstanceID (3.2.1) 和 FirebaseMessaging (3.1.2),我可以很好地获取 APNS 令牌,但每次我尝试获取 FCM 令牌时,它都会出现输出为 nil 或当我使用 InstanceID.instanceID().instanceID(handler:) 时,它会导致一些超时错误代码 1003 和 nil 的结果。 didReceiveRegistrationToken 也不会被调用。我尝试了许多 2017 年的解决方案,但它们要么已被弃用,要么不起作用。设置了 MessageDelegate 和 UNUserNotificationCenterDelegate,在功能中启用了推送通知和钥匙串共享,方法 swizzling 通过 p-list 关闭,这是我的 didRegisterForRemoteNotificationsWithDeviceToken 函数,我在其中正确获取了 apns 令牌。有任何想法吗?谢谢。

`public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("Registration Successful: Device token \(deviceToken)")
    Messaging.messaging().apnsToken = deviceToken
    print("Messaging APNS Token:\(Messaging.messaging().apnsToken)")
    print("Instance Id: \(InstanceID.instanceID().token())") //deprecated & returns nil

    print("Messaging APNS Token:\(Messaging.messaging().apnsToken)")
    print("Token:\(Messaging.messaging().fcmToken)") // return nil
    InstanceID.instanceID().instanceID { (result, error) in
        if let result = result {
             print("Result:\(String(describing: result?.token))")
    } else {
            print("Error:\(error))")
    } //returns after about 2 mins with an firebase iid error of 1003

}

【问题讨论】:

  • 你解决了吗?

标签: ios swift firebase firebase-cloud-messaging


【解决方案1】:

请检查以下应用委托代码。

import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications


class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()



    //REMOTE NOTIFICATION

    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()

    Messaging.messaging().delegate = self

    let token = Messaging.messaging().fcmToken
    print("FCM token: \(token ?? "")")



    //Added Code to display notification when app is in Foreground
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self
    } else {
        // Fallback on earlier versions
    }
    return true
}



func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken as Data
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    // Print full message.
    print(userInfo)
}



// This method will be called when app received push notifications in foreground
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge])
}


// MARK:- Messaging Delegates
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    InstanceID.instanceID().instanceID { (result, error) in
        if let error = error {
            print("Error fetching remote instange ID: \(error)")
        } else if let result = result {
            print("Remote instance ID token: \(result.token)")
        }
    }
}

    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("received remote notification")
    }
}

【讨论】:

  • 这几乎是我已经拥有的。使用“let token = Messaging.messaging().fcmToken”返回 nil。而且我不知道 Reach 功能的用途。我可以获得 APNS 令牌,但 FCM 令牌为零
  • 请忽略到达功能。我也编辑过。还可以尝试删除 pod 并再次添加 pod 'Firebase/Messaging'
  • 'Messaging.messaging().delegate = self' 行应该在 'registerForRemoteNotifications()' 之后
猜你喜欢
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-28
  • 2016-09-24
  • 2016-10-13
  • 2016-10-06
  • 2019-07-16
相关资源
最近更新 更多