Swift read userInfo of remote notification
Solution 1
The root level item of the userInfo
dictionary is "aps"
, not "alert"
.
Try the following:
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let message = alert["message"] as? NSString {
//Do stuff
}
} else if let alert = aps["alert"] as? NSString {
//Do stuff
}
}
See Push Notification Documentation
Solution 2
Swift 5
struct Push: Decodable {
let aps: APS
struct APS: Decodable {
let alert: Alert
struct Alert: Decodable {
let title: String
let body: String
}
}
init(decoding userInfo: [AnyHashable : Any]) throws {
let data = try JSONSerialization.data(withJSONObject: userInfo, options: .prettyPrinted)
self = try JSONDecoder().decode(Push.self, from: data)
}
}
Usage:
guard let push = try? Push(decoding: userInfo) else { return }
let alert = UIAlertController(title: push.aps.alert.title, message: push.aps.alert.body, preferredStyle: .alert)
Solution 3
Method (Swift 4):
func extractUserInfo(userInfo: [AnyHashable : Any]) -> (title: String, body: String) {
var info = (title: "", body: "")
guard let aps = userInfo["aps"] as? [String: Any] else { return info }
guard let alert = aps["alert"] as? [String: Any] else { return info }
let title = alert["title"] as? String ?? ""
let body = alert["body"] as? String ?? ""
info = (title: title, body: body)
return info
}
Usage:
let info = self.extractUserInfo(userInfo: userInfo)
print(info.title)
print(info.body)
Solution 4
For me, when I send the message from Accengage, the following code works -
private func extractMessage(fromPushNotificationUserInfo userInfo:[NSObject: AnyObject]) -> String? {
var message: String?
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let alertMessage = alert["body"] as? String {
message = alertMessage
}
}
}
return message
}
The only difference from Craing Stanford's answer is the key
I used to extract message from alert
instance which is body
which is different. See below for more clearification -
if let alertMessage = alert["message"] as? NSString
vs
if let alertMessage = alert["body"] as? String
Solution 5
This is my version for objC
if (userInfo[@"aps"]){
NSDictionary *aps = userInfo[@"aps"];
if (aps[@"alert"]){
NSObject *alert = aps[@"alert"];
if ([alert isKindOfClass:[NSDictionary class]]){
NSDictionary *alertDict = aps[@"alert"];
if (alertDict[@"message"]){
NSString *message = alertDict[@"message"];
}
}
else if (aps[@"alert"]){
NSString *alert = aps[@"alert"];
}
}
}
![0x52](https://i.stack.imgur.com/ansIn.jpg?s=256&g=1)
0x52
Updated on July 09, 2022Comments
-
0x52 almost 2 years
I implemented a function to open an AlertView when I receive a remote notification like this:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){ var notifiAlert = UIAlertView() var NotificationMessage : AnyObject? = userInfo["alert"] notifiAlert.title = "TITLE" notifiAlert.message = NotificationMessage as? String notifiAlert.addButtonWithTitle("OK") notifiAlert.show() }
But NotificationMessage is always nil.
My json payload looks like this:
{"aps":{"alert":"Testmessage","badge":"1"}}
I am using Xcode 6, Swift and I am developing for iOS8. I searched hours now, but didn't find any useful information. The Notifications works perfectly.. and if I click it, the alertview opens. My problem is, that I am not able to get the data out of userInfo.
-
Andrew Smith about 7 yearsHow would this look in objective C++?
-
lozflan over 6 yearsIts good that you've informed OP the root level item is aps and not alert in the userInfo dic .... but how does one find out the correct keys and dictionary structure that is within an iOS system sent notification userInfo dictionary. in swift 4 documentation i can see notification names all standardised within Notification.Name but i can't see any documentation saying ... this is the userInfo dictionary sent with a particular notification and here are the keys..?
-
Saurabh almost 6 yearsis it possible to add more parameters in aps?