有没有一种方法可以仅更新Swift中本地用户通知的触发日期-Unusernotificationcenter
以下代码成功更新与标识符 notificationID
匹配的本地用户通知。
有没有办法只更新触发日期并保持标题和正文不变?
func updateNotificationWithID(notificationID:String){
let center = UNUserNotificationCenter.current()
var request : UNNotificationRequest?
center.getPendingNotificationRequests{ notifications in
for notificationRequest in notifications{
if notificationRequest.identifier == notificationID{
request = notificationRequest
}
}
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents([.month, .day, .hour, .minute, .second], from: .now.addingTimeInterval(60))
let newTrigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Modified Title"
notificationContent.body = "Modified body"
let updateRequest = UNNotificationRequest(identifier: request!.identifier, content: notificationContent, trigger: newTrigger)
UNUserNotificationCenter.current().add(updateRequest, withCompletionHandler: { (error) in
print("successfully updated")
if error != nil {
print("
The following code successfully updates a local user notification that matches the identifier, notificationID
.
Is there a way to update just the trigger date and leave the title and the body intact?
func updateNotificationWithID(notificationID:String){
let center = UNUserNotificationCenter.current()
var request : UNNotificationRequest?
center.getPendingNotificationRequests{ notifications in
for notificationRequest in notifications{
if notificationRequest.identifier == notificationID{
request = notificationRequest
}
}
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents([.month, .day, .hour, .minute, .second], from: .now.addingTimeInterval(60))
let newTrigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Modified Title"
notificationContent.body = "Modified body"
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)")
}
})
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论