gpt4 book ai didi

swift - UNNotificationContent userInfo 在处理响应时为空,但在发出请求时正确

转载 作者:行者123 更新时间:2023-11-28 07:32:49 25 4
gpt4 key购买 nike

我正在构建桌面 Cocoa 应用程序。当用户单击按钮时,如果资源下载成功,应用程序会向用户发送本地通知。当用户点击通知时,我想打开下载资源源的 URL。我试图将 URL 存储在 userInfoUNMutableNotificationContent 中带有整数键的字典。

我可以看到在添加通知请求之前内容就在那里:[AnyHashable(0): "https://stackoverflow.com/questions/ask"],但是它是空的在委托(delegate)的处理程序中:[:]

// 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)")
}

这是唯一创建通知的地方,我已经验证请求 identifiers 匹配。

notify() 调用者示例

if let url = URL(string: "https://stackoverflow.com/questions/ask") {
notificationDelegate.notify(userInfo: [0: url.absoluteString])
}

最佳答案

UNNotificationContentuserInfo 属性的文档指出键必须是 property list types .这意味着它们必须是可直接存储在属性列表中的简短类型列表之一。此列表中的类标量类型包括 NSNumberNSStringNSDate

据我所知,您用作 key 的文字 Swift Int 0 应该 自动桥接到 NSNumber 因此作为键是合法的。这似乎没有发生。

您将不得不直接使用其中一种 plist 类型。如果你想要一个数字作为键,0 as NSNumber 应该可以工作(希望如此?),或者 NSNumber(value: 0)。我认为,更常见的情况是,键是一个字符串。

我觉得这个值filing a radar about ,特别是因为 Swift String 显然 正确且自动桥接(到 NSString)。 (ObjC 方面的异常让我们知道字典无法编码,而不是无声消失,这也很好......)

关于swift - UNNotificationContent userInfo 在处理响应时为空,但在发出请求时正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54175509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com