我在访问[AnyHashable(“ aps”)]中的键警报的值时遇到问题。我正在尝试将通知警报存储为字符串,以便可以执行进一步的操作,但是我无法访问警报值。你们知道我在做什么错吗?
我是新手,我很快就尝试通过构建自己的应用程序弄脏我的手。感谢您的帮助!
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
completionHandler([.sound, .alert])
}
实际输出:
[
AnyHashable("gcm.message_id"): 0:1559231429586204%16146ddd16146ddd,
AnyHashable("google.c.a.ts"): 1559231429,
AnyHashable("aps"): {
alert = "BBQ event on 6/15";
sound = enabled;
},
AnyHashable("gcm.n.e"): 1,
AnyHashable("google.c.a.udt"): 0,
AnyHashable("gcm.notification.sound2"): enabled,
AnyHashable("google.c.a.e"): 1,
AnyHashable("google.c.a.c_id"): 5464147931275974726
]
预期输出:
"BBQ event on 6/15"
答案 0 :(得分:0)
您需要首先将aps
的值提取为Dictionary
类型的[AnyHashable:Any]
。然后从alert
中获取apsDict
的值,即
if let apsDict = userInfo["aps"] as? [AnyHashable:Any] {
if let alert = apsDict["alert"] {
print("Alert: \(alert)")
}
}