有没有一种方法可以仅更新Swift中本地用户通知的触发日期-Unusernotificationcenter

发布于 2025-01-17 14:58:36 字数 1287 浏览 0 评论 0原文

以下代码成功更新与标识符 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文