FCM令牌不要调用didReceiveRegistrationToken

时间:2019-07-10 10:26:36

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

您好,在我的应用程序中使用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)
    }  
}

2 个答案:

答案 0 :(得分:0)

 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的方法调用

答案 1 :(得分:0)

我使用以下功能正常工作

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