首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法更新本地计划的通知内容

无法更新本地计划的通知内容
EN

Stack Overflow用户
提问于 2017-07-11 11:56:20
回答 2查看 4.9K关注 0票数 5

在一个WWDC会话中,我得到了用于更新现有通知的代码片段。我觉得不管用。试图更新通知内容。

首先,我从UNUserNotificationCenter请求挂起的通知,这总是有效的。然后,我将创建新的请求,用现有的唯一标识符更新通知。

有一个新变量content: String

代码语言:javascript
运行
复制
// 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。

读克里斯的答案,试图与第一选择。

代码语言:javascript
运行
复制
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和调度之后,仍然给我旧的通知版本。我不得不在调用removePendingNotificationRequestscenter.add(request)之间增加1秒的延迟。

标记克里斯的答案被接受,但可以分享更好的选择。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-11 14:15:46

问题是,您没有修改现有的通知,而是添加了一个带有重复标识符的新通知。

让我们首先处理重复问题,这个重复通知没有出现的原因是标识符不是唯一的。来自文档

(如果标识符不是唯一的,则通知不传递)。

你有两个选择。您可以修改现有的通知,或者2)删除它并添加新的通知。

对于1,您已经有了请求,而不是从请求中扣动触发器和标识符,只需用更新的request.content替换notificationContent即可。

对于2,只需在添加之前添加一行:

代码语言:javascript
运行
复制
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [request!.identifier])
票数 4
EN

Stack Overflow用户

发布于 2017-07-11 15:14:36

在我请求允许通知之后:

我直接从我的viewDidLoad触发一个通知,但也会触发另一个具有相同标识符的通知。最后,将显示更新后的show /updatedTitle。

代码语言:javascript
运行
复制
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()
    }

我的更新函数

代码语言:javascript
运行
复制
    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。

用于处理传入通知的

代码语言:javascript
运行
复制
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])

    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45033911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档