【问题标题】:Why can't I read push message data in Swift5?为什么我不能在 Swift5 中读取推送消息数据?
【发布时间】:2019-09-26 06:09:14
【问题描述】:

我有一个问题,我此时无法读取推送消息数据。我正常收到消息。

当我按下主页按钮时,我可以在应用程序处于前台或后台时同时接收。

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

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 中发送测试消息

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

【问题讨论】:

  • 我在您的数据中没有看到关键的“警报”,因此使用了 swift 字典而不是 NSDictionary
  • 嗨@JoakimDanielson 没错,但我什至看不到完整数据。 'Log.Info(数据)'
  • 将该日志语句移到函数的开头,如果保护语句失败,它将退出,其余的不执行
  • @JoakimDanielson 请看我修改的问题。
  • @JoakimDanielson 除了应用程序功能外,我还有两个消息功能。这没有必要吗?

标签: ios swift firebase push-notification


【解决方案1】:

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

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

通知框架从 iOS 10 开始用更新的方法替换了这个方法:

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

通知框架的更多详细信息可在here获取。

【讨论】:

    【解决方案2】:

    这使用不同的函数解决了这个问题。

    我使用不同的函数解决了它,但我希望有另一个好的解决方案。

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

        @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()
        }
    

    【讨论】:

    • 这不是替代方案,这是iOS10以来应该这样做的方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2023-02-06
    • 1970-01-01
    • 2017-12-28
    • 2013-01-26
    • 2022-06-17
    • 1970-01-01
    相关资源
    最近更新 更多