I am building a Desktop Cocoa App. When the user clicks a button, if a resource is successfully downloaded, the application sends a local notification to the user. When the user click's the notification, I want to open the URL to the source of the downloaded resource. I am attempting to store the URL in the userInfo
dictionary in UNMutableNotificationContent
with an integer key.
I can see that the content is there before the notification request is added: [AnyHashable(0): "https://stackoverflow.com/questions/ask"]
, but it is empty in the delegate's handler: [:]
// helper method to create the notification
func notify(userInfo: [AnyHashable : Any] = [:]) {
let uid = UUID().uuidString
let content = UNMutableNotificationContent()
content.title = self.title
content.userInfo = userInfo
content.sound = UNNotificationSound.default
print("add notification userInfo \(content.userInfo)")
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: uid, content: content, trigger: trigger)
center.add(request) { (error) in
print("add notification error \(error)")
}
}
// notification click handler
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("recieved notification userInfo: \(response.notification.request.content.userInfo)")
}
This is the only place notifications are being created, and I've verified that the request identifiers
match.
notify() caller example
if let url = URL(string: "https://stackoverflow.com/questions/ask") {
notificationDelegate.notify(userInfo: [0: url.absoluteString])
}