切换到 UNNotification 时,不再显示任何通知
While switching to UNNotification, no notification shows up anymore
我最近切换到 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())")
}
}
当我看到带有该代码的待处理通知时…
let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests { requests in
for request in requests {
print(request)
}
}
…iPhone(或模拟器)上没有显示任何通知,也没有设置错误。
我发现绝对需要按如下方式设置主体:
content.body = "a body"
这样,我的通知又回来了。
我最近切换到 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())")
}
}
当我看到带有该代码的待处理通知时…
let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests { requests in
for request in requests {
print(request)
}
}
…iPhone(或模拟器)上没有显示任何通知,也没有设置错误。
我发现绝对需要按如下方式设置主体:
content.body = "a body"
这样,我的通知又回来了。