【问题标题】:FCM token don't call didReceiveRegistrationTokenFCM 令牌不调用 didReceiveRegistrationToken
【发布时间】:2019-11-19 23:08:51
【问题描述】:

您好,在我的应用程序中使用 FCM

当我尝试读取 FCM 令牌时,即使编译器也不调用此委托方法,也请帮忙。

当我使用 ios11 swift4 检查此方法时不要调用我

extension AppDelegate: MessagingDelegate{

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")

        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
    }

    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("Message data:", remoteMessage.appData)
    }  
}

【问题讨论】:

    标签: ios swift firebase push-notification firebase-cloud-messaging


    【解决方案1】:
     didReceiveRegistrationToken
    

    是一个委托函数。它将在收到来自 firebase 的令牌时被调用。如果它没有被调用,请确保您在 AppDelegate 中遵循了代码

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        Messaging.messaging().delegate = self
        return true
    }
    

    我们使用Messaging.messaging().delegate = self 注册AppDelegate 以接收来自 Firebase 的方法调用

    【讨论】:

    • 我的错误是 FirebaseApp.configure() 晚于 Messaging.messaging().delegate = self 。根据上面的代码更改它解决了我的问题,谢谢
    • 正是我的应用发生了什么。不知不觉中,我在设置消息代理后移动了configureApp 调用。使用正确的顺序解决了这个问题。
    【解决方案2】:

    我使用下面的功能它工作正常

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            if let refreshedToken = InstanceID.instanceID().token() {
               print("InstanceID token: \(refreshedToken)")
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2020-09-24
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多