In one of the WWDC sessions I got code snippet for updating existing notifications. I don't think it works. Trying to update notification content.
First I request pending notifications from UNUserNotificationCenter
which always works. Then I am creating new request to update notification with existing unique identifier.
There's 1 new variable 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)")
}
})
I am unable to catch error. The problem is that notification content body doesn't change.
Update.
I also tried to change trigger with different repeat interval. It doesn't work, notification is repeated with the same original interval it was created with.
Update 2.
Read Chris' answer, trying to go with first option.
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.
}
}
})
As you can see I can't modify original request.
Update 3.
Had go with second "delete first" option. Noticed that calling removePendingNotificationRequests
and schedule after, still gives me old notification version. I had to add 1 second delay between calling removePendingNotificationRequests
and center.add(request)
.
Marked Chris' answer as accepted but feel free to share better option.
content
? Can you print it and make sure it's what you expect? – CrosspatchnotificationContent.body
andcontent
by giving the content a different name. Just write it asnotificationContent.body = body
because overtime you may have the exact confusion I just had. the left side is a property of another, the right Side is a local variable—enough for others to know the differences but also know the similarities. But again can you print and see if the body/title are what you expect? Let me know the result. Otherwise your code so far looks fine. I've also pasted in my own Xcode to give it a try. – Crosspatchidentifier
and it updated the previous notification. To do such, you don't need to remove the pending notification. Adding the new NotificationRequest to the NotificationsCenter is all you need to do. I highly suggest you see this answer and download the sample project. It's a very very good sample project – Crosspatchrequest.content = notificationContent
tolet updateRequest = UNNotificationRequest(identifier: notificationIdentifier, content: notificationContent, trigger: newTrigger)
andUNUserNotificationCenter.current().add(updateRequest, withCompletionHandler: { (error) in print("successfully updated") if error != nil { print("🚫 Couldn't update notification \(error!.localizedDescription)") }
. Basically grab – Crosspatch