【发布时间】:2017-09-22 17:04:12
【问题描述】:
我正在使用 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)
【问题讨论】:
-
当通知出现在您的屏幕上时,它是什么样的?可以加个截图吗?你点击哪个动作?
-
您的比较有误。您的操作标识符是“mycustomactionidentifier”,但您已检查为“mycustomidentifier”。因此编译器只是忽略了你的动作标识符。
-
嗨@Honey,这是一个常规通知,标题和正文是我选择的。无论我在哪里单击通知,它都会返回正确的 categoryIdentifier 但错误的 actionIdentifier!
-
和@Mannopson,对不起,这是一个错字,但在我的代码中它是正确的,但仍然无法正常工作。谢谢!
标签: ios swift ios10 unusernotificationcenter