iOS:FIRMessaging接收到数据消息,但FIRMessagingDelegate的消息:didReceiveMessage:未实现

时间:2018-10-20 05:40:16

标签: ios swift apple-push-notifications firebase-cloud-messaging

我已经从Firebase配置了推送通知。当我从Firebase控制台发送它时,一切都很好,但是当我从后端发送它时,没有方法仅在日志中被称为错误-FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented

相同的推送在Android上有效,但在iOS上无效。

这是我的代码:

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

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (isGranted, err) in
        if err != nil {
            //Something bad Happend
        } else {
            UNUserNotificationCenter.current().delegate = self
            Messaging.messaging().delegate = self

            DispatchQueue.main.async {
                application.registerForRemoteNotifications()
            }
        }
    }

    UIApplication.shared.registerForRemoteNotifications()
    FirebaseApp.configure()

    return true
}

 public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
    let info = notification.request.content.userInfo
    print(info)  
}

func convertToJson(text: String) -> [String: Any]? {
    if let data = text.data(using: .utf8) {
        do {
            return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
        } catch {
            print(error.localizedDescription)
        }
    }
    return nil
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    // DO NOTHING
    print(userInfo)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // If you are receiving a notification message while your app is in the background,
     print(userInfo)
}

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    let newToken = InstanceID.instanceID().token()
    print(newToken)
    let loggedUser = UserDefaults.standard
    loggedUser.set(newToken, forKey: Constants.deviceToken)
    connectToFCM()
}

0 个答案:

没有答案