5

在其中一个 WWDC 会议中,我获得了用于更新现有通知的代码片段。我不认为它有效。正在尝试更新通知内容。

首先,我请求UNUserNotificationCenter始终有效的待处理通知。然后我正在创建新请求以使用现有的唯一标识符更新通知。

有 1 个新变量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)")
    }
})

我无法捕捉到错误。问题是通知内容主体 没有改变。

更新。

我还尝试使用不同的重复间隔更改触发器。它不起作用,通知以创建时的相同原始间隔重复。

更新 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和之间添加 1 秒延迟center.add(request)

将克里斯的回答标记为已接受,但请随时分享更好的选择。

4

2 回答 2

4

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

让我们先解决重复问题,这个重复通知没有显示的原因是因为标识符不是唯一的。从文档

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

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

对于 1,您已经有了请求,而不是从中拉出触发器和标识符,只需将 request.content 替换为您更新的通知内容。

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

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [request!.identifier])
于 2017-07-11T14:15:46.807 回答
1

在我请求允许通知之后:

我从我那里触发了一个通知,viewDidLoad但随后也触发了另一个具有相同标识符的通知。最后,updatedBody/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()不会有任何区别。我仍然会收到更新的通知。

用于处理传入通知

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])

    }
}
于 2017-07-11T15:14:36.253 回答