我正在使用UNUserNotificationCenterDelegate(> ios 10)和其中一个委托方法,我可以检查来自通知的响应总是actionIdentifier等于“com.apple.UNNotificationDefaultActionIdentifier”,无论我做什么 . “response.notification.request.content.categoryIdentifier”是正确的,具有预期值,但request.actionIdentifier永远不会正确(下面的示例中为“mycustomactionidentifier”) . 有谁知道我错过了什么?
extension NotificationManager: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
completionHandler([.alert,.sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
if response.notification.request.content.categoryIdentifier == "TEST" {
if response.actionIdentifier == "mycustomactionidentifier" {
NSLog("it finally works dude!")
}
}
completionHandler()
}
}
我将操作和类别添加到通知中心:
let uploadAction = UNNotificationAction(identifier: "mycustomactionidentifier", title: "Uploaded", options: [])
let category = UNNotificationCategory(identifier: "TEST", actions: [uploadAction], intentIdentifiers: [])
center.setNotificationCategories([category])
并发送请求正确的标识符:
let uploadContent = UNMutableNotificationContent()
uploadContent.title = String(number) + " asset(s) added"
uploadContent.body = "Check your inventory to manage your assets!"
uploadContent.categoryIdentifier = "TEST"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 6, repeats: false)
let uploadRequestIdentifier = "mycustomactionidentifier"
let uploadRequest = UNNotificationRequest(identifier: uploadRequestIdentifier, content: uploadContent, trigger: trigger)
UNUserNotificationCenter.current().add(uploadRequest, withCompletionHandler: nil)
2 回答
首先:注册您的自定义操作:
并使用唯一标识符创建内容:
最后:使用默认和自定义操作处理通知 .
希望能帮助到你!
Second Edition
结果如下:这将是我们的
UNNotificationDefaultActionIdentifier
:这个是通知的扩展版本,我们可以处理这两个动作:
正如Mannopson所说,您可以注册默认操作标识符 . 然而,我虽然你需要的是另一件事:
response.notification.request.identifier
来自Apple's actionIdentifier description表示此参数可能包含一个UNNotificationAction对象的标识符,或者它可能包含系统定义的标识符 . 这意味着你需要注册一个,希望我是对的(因为我是swift的新手)