当通过推送通知接收时,AnyHashable:Any 不会转换为 swift 中的字典
AnyHashable:Any doesn't convert to dictionary in swift when receives through push notification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
startSavingNotification(userInfo: userInfo)
completionHandler(UIBackgroundFetchResult.newData)
//TODO: TEST
//showTestPushAlert(userInfo: userInfo)
}
//Fire Test notification
func startSavingNotification(userInfo:[AnyHashable : Any]) {
//Fetch Payload Dict
if let payloadDict = userInfo["payload"] as? Dictionary<String,Any> {
savePushNotification(payloadDict: payloadDict)
}
}
func showTestPushAlert(userInfo:[AnyHashable : Any]) {
let alert = UIAlertController(title:"", message: "\(userInfo)", preferredStyle: .alert)
let cancelButton = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alert.addAction(cancelButton)
UIApplication.topViewController()?.present(alert, animated: true, completion: nil)
}
当我尝试在警报中显示数据时,此用户信息看起来像这样:
如果条件为假,请在此处做错事:??
如果让 payloadDict = userInfo["payload"] 作为?字典{
savePushNotification(payloadDict:payloadDict)
}
//This method will parse push notification userinfo data
func parseNotification(userInfo: [AnyHashable: Any]) {
print(">>>parseNotificationCalled ")
if let notification = userInfo["payload"] as? String,
let jsonData = notification.data(using: .utf8),
let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as? NSDictionary {
//This is the point where we need to save push notification
savePushNotification(payloadDict: dict ?? [:])
print("APS PAYLOAD DICTIONARY \(dict)")
}
在我的例子中,有效载荷字典以字符串格式出现。已解决。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
startSavingNotification(userInfo: userInfo)
completionHandler(UIBackgroundFetchResult.newData)
//TODO: TEST
//showTestPushAlert(userInfo: userInfo)
}
//Fire Test notification
func startSavingNotification(userInfo:[AnyHashable : Any]) {
//Fetch Payload Dict
if let payloadDict = userInfo["payload"] as? Dictionary<String,Any> {
savePushNotification(payloadDict: payloadDict)
}
}
func showTestPushAlert(userInfo:[AnyHashable : Any]) {
let alert = UIAlertController(title:"", message: "\(userInfo)", preferredStyle: .alert)
let cancelButton = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alert.addAction(cancelButton)
UIApplication.topViewController()?.present(alert, animated: true, completion: nil)
}
当我尝试在警报中显示数据时,此用户信息看起来像这样:
如果条件为假,请在此处做错事:?? 如果让 payloadDict = userInfo["payload"] 作为?字典{ savePushNotification(payloadDict:payloadDict) }
//This method will parse push notification userinfo data
func parseNotification(userInfo: [AnyHashable: Any]) {
print(">>>parseNotificationCalled ")
if let notification = userInfo["payload"] as? String,
let jsonData = notification.data(using: .utf8),
let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as? NSDictionary {
//This is the point where we need to save push notification
savePushNotification(payloadDict: dict ?? [:])
print("APS PAYLOAD DICTIONARY \(dict)")
}
在我的例子中,有效载荷字典以字符串格式出现。已解决。