我最近切换到 UNNotification 并想出了这个升级代码:
let content = UNMutableNotificationContent()
content.title = " \(story.title)"
content.userInfo = ["Story.link": story.link, "Story.type": "HotNews"]
let request = UNNotificationRequest(identifier: "NEW", content: content, trigger: nil)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
//handle eror here
print("*** Error presentHotNewsNotification(): \(error) (\(story.dumpProperties()))")
}
else {
story.notified = true
//story.saveContext
print("presentHotNewsNotification() for story \(story.dumpProperties())")
}
}
当我看到带有该代码的待处理通知时……</p>
let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests { requests in
for request in requests {
print(request)
}
}
…iPhone(或模拟器)上没有显示通知,也没有设置错误。