ios FCM消息传递didReceiveRemoteMessage未调用

时间:2018-02-04 23:13:07

标签: ios firebase firebase-cloud-messaging

我想使用FCM向iOS设备发送通知和数据消息。

一切正常,但由于某种原因,永远不会调用委托方法messaging didReceiveRemoteMessage。因此,当应用程序位于前台时,我无法获取数据消息...我在数据旁边有一个通知块和没有通知块的情况下尝试了它。所以我发送的消息是这样的:

{'message': {
    'token': 'mytokenstandshere',
    'notification': {
        'body': 'message',
        'title': 'title'},
    'data': {
        'datafield': 'datavalue',
        'datafield2': 'datavalue2'}
}}

我尝试了所有可能性(没有通知,没有数据,两者都有)。通知工作没有问题,但数据块没有出现在干净的数据消息中。

我只想让它在iOS 11+上运行。但我甚至尝试使用谷歌的文档中的iOS 9和10部分。

这是我的AppDelegate:

@

UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        Hardware.start()

        Preferences.initDefaults()

        FirebaseApp.configure()

        Messaging.messaging().delegate = self

        Messaging.messaging().shouldEstablishDirectChannel = true

        UNUserNotificationCenter.current().delegate = self

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })

        application.registerForRemoteNotifications()


        return true
    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("new Token: \(fcmToken)")
        Messaging.messaging().subscribe(toTopic: TOPIC_ALL_MESSAGES)

        NotificationSettingsListener.start()
    }

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

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        completionHandler(UIBackgroundFetchResult.newData)

    }

}

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

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

        // Print full message.
        print(userInfo)

        // Change this to your preferred presentation option
        completionHandler([.alert])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo

        // Print full message.
        print(userInfo)

        completionHandler()
    }
}

2 个答案:

答案 0 :(得分:0)

我没有看到:

application(_: didReceiveRemoteNotification: fetchCompletionHandler:)

我认为这是处理所必需的。 Reference

答案 1 :(得分:0)

请确保您必须在AppDelegate.m

中分配Firebase消息传递委托
[FIRMessaging messaging].delegate = self;