I am trying to obtain the alert message details when my app is opened when the user clicks on the Notification Alert message on his MAC.
I have no problem getting the Alert when the app is opened. The following delegate works fine for when the App is already opened.
func application(_ application: NSApplication,
didReceiveRemoteNotification userInfo: [String : Any])
However when the App is opened via the clicking od the Alert Notification I have to get the userInfo from
func applicationDidFinishLaunching(_ aNotification: Notification)
I am unable to retrieve the alert userInfo
in this function.
let mydictionary = aNotification.userInfo
let mydesc = mydictionary!.description
os_log("aNotification.userInfon= %{public}@", log: osLog, mydesc)
The output from os_log
, I can see the alert message in the description.
aNotification.userInfon= [AnyHashable("NSApplicationLaunchIsDefaultLaunchKey"): 0, AnyHashable("NSApplicationLaunchUserNotificationKey"): <UNNotificationResponse: 0x600003ac1480; actionIdentifier: com.apple.UNNotificationDefaultActionIdentifier, notification: <UNNotification: 0x600003ac1580; date: 2019-05-08 18:26:59 +0000, request: <UNNotificationRequest: 0x6000034cd560; identifier: E6D76BF3-6643-4627-BF38-3B11B5C3F0B3, content: <UNNotificationContent: 0x600000fe0780; title: (null), subtitle: (null), body: (14) - iPhone - Tom Owen, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: , launchImageName: , threadIdentifier: , attachments: ( ), badge: 0, sound: <UNNotificationSound: 0x600001ee92c0>,, trigger: <UNPushNotificationTrigger: 0x6000038fa780; contentAvailable: NO, mutableContent: NO>>>>]
I have tried many ways to access the "dictionary", the following is one way.
if let aps = aNotification.userInfo!["aps"] as? NSDictionary {
os_log(" aNotification.userInfo ok", log: osLog)
if let alert = aps["alert"] as? NSDictionary {
os_log("alert ok", log: osLog)
if let alertMessage = alert["body"] as? String {
os_log("body ok", log: osLog)
message = alertMessage
}
}
} else {
os_log("no aNotification.userInfo!", log: osLog)
}
However I keep getting "no aNotification.userInfo!"
How should I be accessing "body: (14) - iPhone - Tom Owen" from userInfo
?
Please have a closer look at the log. You have to get the user notification with the key launchUserNotificationUserInfoKey
. The value for that key is an UNNotificationResponse
instance.
You get the body
with
func applicationDidFinishLaunching(_ notification: Notification)
if let response = notification.userInfo?[NSApplication.launchUserNotificationUserInfoKey] as? UNNotificationResponse {
let content = response.notification.request.content
let body = content.body
message = body
}
}
However didReceiveRemoteNotification
passes the traditional userInfo
dictionary
func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
if let aps = userInfo?["aps"] as? [String:Any] { // please use Swift native types
os_log(" aNotification.userInfo ok", log: osLog)
if let alert = aps["alert"] as? [String:Any] {
os_log("alert ok", log: osLog)
if let alertMessage = alert["body"] as? String {
os_log("body ok", log: osLog)
message = alertMessage
}
}
} else {
os_log("no aNotification.userInfo!", log: osLog)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.