在一个WWDC会话中,我得到了用于更新现有通知的代码片段。我觉得不管用。试图更新通知内容。
首先,我从UNUserNotificationCenter
请求挂起的通知,这总是有效的。然后,我将创建新的请求,用现有的唯一标识符更新通知。
有一个新变量content: String
。
// Got at least one pending notification.
let triggerCopy = request!.trigger as! UNTimeIntervalNotificationTrigger
let interval = triggerCopy.timeInterval
let newTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: true)
// Update notificaion conent.
let notificationContent = UNMutableNotificationContent()
notificationContent.title = NSString.localizedUserNotificationString(forKey: "Existing Title", arguments: nil)
notificationContent.body = content
let updateRequest = UNNotificationRequest(identifier: request!.identifier, content: notificationContent, trigger: newTrigger)
UNUserNotificationCenter.current().add(updateRequest, withCompletionHandler: { (error) in
if error != nil {
print(" Couldn't update notification \(error!.localizedDescription)")
}
})
我找不到错误。问题是通知内容body不会改变。
更新。
我还试图用不同的重复间隔来改变触发器。它不起作用,通知会以与创建时相同的原始间隔重复。
更新2。
读克里斯的答案,试图与第一选择。
let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests(completionHandler: { (requests) in
for request in requests {
if request.identifier == notificationIdentifier {
// Got at least one pending notification,
// update its content.
let notificationContent = UNMutableNotificationContent()
notificationContent.title = NSString.localizedUserNotificationString(forKey: "new title", arguments: nil)
notificationContent.body = "new body"
request.content = notificationContent // ⛔️ request.content is read only.
}
}
})
如您所见,我不能修改原始请求。
更新3。
有第二个“删除第一个”选项。注意到呼叫removePendingNotificationRequests
和调度之后,仍然给我旧的通知版本。我不得不在调用removePendingNotificationRequests
和center.add(request)
之间增加1秒的延迟。
标记克里斯的答案被接受,但可以分享更好的选择。
发布于 2017-07-11 14:15:46
问题是,您没有修改现有的通知,而是添加了一个带有重复标识符的新通知。
让我们首先处理重复问题,这个重复通知没有出现的原因是标识符不是唯一的。来自文档:
(如果标识符不是唯一的,则通知不传递)。
你有两个选择。您可以修改现有的通知,或者2)删除它并添加新的通知。
对于1,您已经有了请求,而不是从请求中扣动触发器和标识符,只需用更新的request.content替换notificationContent即可。
对于2,只需在添加之前添加一行:
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [request!.identifier])
发布于 2017-07-11 15:14:36
在我请求允许通知之后:
我直接从我的viewDidLoad
触发一个通知,但也会触发另一个具有相同标识符的通知。最后,将显示更新后的show /updatedTitle。
import UIKit
import UserNotifications
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let content = UNMutableNotificationContent()
content.title = "Scheduled Task"
content.body = "dumbBody"
content.badge = 1
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "alertCategory"
UNUserNotificationCenter.current().delegate = self
//Setting time for notification trigger
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 3.0, repeats: false)
let request = UNNotificationRequest(identifier:"myIdentifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: {_ in print(" was registered")})
updateNotification()
}
我的更新函数
func updateNotification(){
let center = UNUserNotificationCenter.current()
var request : UNNotificationRequest?
center.getPendingNotificationRequests{ notifications in
for notificationRequest in notifications{
if notificationRequest.identifier == "myIdentifier"{
request = notificationRequest
center.removeAllPendingNotificationRequests() // Removing this line or keeping it makes NO difference
}
}
let newTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false)
// Update notificaion conent.
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "UpdatedTitle"
notificationContent.body = "updatedBody"
let updateRequest = UNNotificationRequest(identifier: request!.identifier, content: notificationContent, trigger: newTrigger)
UNUserNotificationCenter.current().add(updateRequest, withCompletionHandler: { (error) in
print("successfully updated")
if error != nil {
print(" Couldn't update notification \(error!.localizedDescription)")
}
})
}
}
}
在上面的片段中:删除center.removeAllPendingNotificationRequests()
没有什么区别。不过,我还是会收到updatedNotification。
用于处理传入通知的
extension ViewController:UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("original identifier was : \(response.notification.request.identifier)")
print("original body was : \(response.notification.request.content.body)")
print("Tapped in notification")
switch response.actionIdentifier {
default:
print("some action was clicked")
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Notification being triggered")
completionHandler( [.alert,.sound,.badge])
}
}
https://stackoverflow.com/questions/45033911
复制相似问题