userInfo通知数据无法检索?

时间:2016-12-07 18:32:05

标签: swift xcode push-notification apple-push-notifications

所以我一直试图弄清楚为什么userInfo没有显示我想要的数据。 当我打印userInfo时,我得到了这个:

[AnyHashable("aps"): {
    alert = "test account \ni'm running late";
    badge = 11;
    sound = "bingbong.aiff";
}]

然而,当我尝试userInfo["alert"]时,它返回nil。我已尝试过包括userInfo["aps"]["alert"]在内的所有内容,但仍然没有。我已经被困了好几个小时了。我希望有人能帮帮忙。

这是我的全部功能:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    //PFPush.handle(userInfo)
    if (userInfo["badge"] != nil) {
        let badgeNumber: Int = userInfo["badge"] as! Int
        application.applicationIconBadgeNumber = badgeNumber
    }
    if application.applicationState == .inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(inBackground: userInfo, block: nil)
    } else if application.applicationState == .active {
        let notificationManager = LNRNotificationManager()
        notificationManager.showNotification(title: "Test", body: "", onTap: { () -> Void in
            notificationManager.dismissActiveNotification(completion: { () -> Void in
                print("Notification dismissed")
            })
        })
        print(userInfo)
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "didRecieveNotificationWhileInApp"), object: nil)
    }
}

1 个答案:

答案 0 :(得分:4)

正如您在userInfo转储badge中看到的那样,在密钥aps的词典中

if let aps = userInfo["aps"] as? [String:Any] {
  let badgeNumber = aps["badge"] as! Int
  application.applicationIconBadgeNumber = badgeNumber
}