为什么我无法在Swift5中读取推送消息数据?

时间:2019-09-26 06:09:14

标签: ios swift firebase push-notification

我有一个问题,我目前无法读取推送消息数据。我通常会收到一条消息。

当我按下“主页”按钮时,当应用程序处于“前景”或“背景”状态时,我都可以接收到。

但是我在日志中看不到消息数据。

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate  {
...
    func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable: Any]) {
      // If you are receiving a notification message while your app is in the background,
      // this callback will not be fired till the user taps on the notification launching the application.
      // TODO: Handle data of notification

      // With swizzling disabled you must let Messaging know about the message, for Analytics
       guard
           let aps = data[AnyHashable("notification")] as? NSDictionary,
           let alert = aps["alert"] as? NSDictionary,
           let body = alert["body"] as? String,
           let title = alert["title"] as? String
           else {
               // handle any error here
               return
           }

       Log.Info("Title: \(title) \nBody:\(body)")
       Messaging.messaging().appDidReceiveMessage(data)

      // Print full message.
      Log.Info(data)
    }
...



    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

          Log.Info("fcmToken \(fcmToken)")
       }

    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
           Log.Info("remort \(remoteMessage.appData)")
    }

我发送的数据

{

     notification : {

                            "title" : "test title.",  

                            "body" : "test context."            

                      },

     data : {

                    "image" : "http://11.111.111.111:100000000/_img/sample_01.jpg",  

                    "page_url" : "http://11.111.111.111:100000000/Point?address=",   

                    "type" : "point"       

             }

}

无法使用任何功能查看日志。我想念什么?请让我知道怎么了。


编辑


我修改了错误的数据并更改了日志的位置。但是我没有任何日志。

AppDelegate.swift

    func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable: Any]) {
      // If you are receiving a notification message while your app is in the background,
      // this callback will not be fired till the user taps on the notification launching the application.
      // TODO: Handle data of notification

      // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Print full message.
        Log.Info(data)
       guard
           let aps = data[AnyHashable("notification")] as? NSDictionary,
           let body = aps["body"] as? String,
           let title = aps["title"] as? String
           else {
               // handle any error here
               return
           }

       Log.Info("Title: \(title) \nBody:\(body)")
       Messaging.messaging().appDidReceiveMessage(data)
    }

在Firebase中发送测试消息

enter image description here

除应用程序的messaging个功能外,我还有两个application个功能。我不需要这些消息功能吗?这些功能从未向我显示日志。

2 个答案:

答案 0 :(得分:1)

您正在使用的方法已在iOS 10中弃用。

https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623117-application

Notifications框架用iOS 10以后的更新方法替换了此方法:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) 

有关通知框架的更多详细信息,请参见here

答案 1 :(得分:0)

这使用其他功能解决了这个问题。

我使用其他函数解决了这个问题,但我希望有另一个好的解决方案。

我想知道根本的问题是什么。这只是另一种选择。

    @available(iOS 10, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let data = response.notification.request.content.userInfo
        guard
            let aps = data[AnyHashable("aps")] as? NSDictionary,
            let alert = aps["alert"] as? NSDictionary,
            let body = alert["body"] as? String
            else {
                Log.Error("it's not good data")
                return
        }


        Log.Info(body)

        completionHandler()
    }