swift ios如何在swift中访问AnyHashable数据

时间:2018-04-12 05:53:52

标签: ios swift hash

如何在swift中访问任何AnyHashable数据我有下面的数据,下面是我点击通知时的日志。当我点击notif和我希望打印或获取数据日志时,我可以在视图上加载它。它已经工作了,当我点击notif我想要的是如何在视图中加载它时记录。

我做了什么

 guard
            let aps = userInfo[AnyHashable("aps")] as? NSDictionary,
            let alert = aps["alert"] as? NSDictionary,
            let body = alert["body"] as? String,
            let title = alert["title"] as? String
            else {
                return
        }

        print("Title: \(title) \nBody:\(body)")

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

数据

[AnyHashable("aps"): {
    alert =     {
        body = "yuuu - 5.00";
        title = "New Chore!";
    };
    "content-available" = 1;
}, AnyHashable("data"): {
    chore =     {
        desc = huu;
        name = yuuu;
        pk = 125;
        reward = "5.00";
        sched = "2018-04-12T09:52:13+08:00";
    };
    "notification_id" = 16;
    pusher =     {
        publishId = "pubid-01ff965a-20af-4a58-9901-89782043d832";
    };
}]

3 个答案:

答案 0 :(得分:1)

anwer:关于如何在点击

时加载notif数据
func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {

        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo["gcmMessageIDKey"] {
            print("Message ID: \(messageID)")
        }
        switch response.actionIdentifier {
        case "action1":
            print("Action First Tapped")
        case "action2":
            print("Action Second Tapped")
        default:
            break
        }

        // Print full message.
        print(userInfo)
        Messaging.messaging().appDidReceiveMessage(userInfo)
        completionHandler()
    }

答案 1 :(得分:0)

可以按如下方式检查APS数据。如果特定密钥在通知数据中不可用,您可以按如下方式检查每个密钥,以避免崩溃应用程序。

if (userInfo["aps"] as? [String:Any]) != nil {
   if let data = userInfo["data"] as? String{
      if let desc = userInfo["desc"] as? String {
          //Access variable desc here...
          print(desc)
      } 
   }
}

答案 2 :(得分:0)

您可以尝试:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

    if application.applicationState == .active{
        let strTitle = (userInfo["aps"] as! NSDictionary)
        let title = strTitle.value(forKeyPath: "alert.title")
        let body = strTitle.value(forKeyPath: "alert.body")
        let alert = UIAlertController(title: "\(title!)", message: "\(String(describing: body))", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default) { action in
                   })
        self.window?.rootViewController?.present(alert, animated: true)
    }else if application.applicationState == .background{
        let strTitle = (userInfo["aps"] as! NSDictionary).value(forKey: "notification") as! NSDictionary
        let title = strTitle.value(forKeyPath: "alert.title")
        let body = strTitle.value(forKeyPath: "alert.body")
    }else{
       // AppUtils.showAlertWithTitle(title: "Hello", message: "Hello")
        let strTitle = (userInfo["aps"] as! NSDictionary).value(forKey: "notification") as! NSDictionary
        let title = strTitle.value(forKeyPath: "alert.title")
        let body = strTitle.value(forKeyPath: "alert.body")
    }
}