我正在使用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)
當通知進入你的屏幕上它看起來像什麼?你能添加截圖嗎?你點擊了哪個動作? – Honey
你有一個錯誤的比較。您的操作標識符是「mycustomactionidentifier」,但您已選中「mycustomidentifier」。因此編譯器會簡單地忽略你的動作標識符 – Mannopson
嗨@Honey,它來作爲一個正常的通知,與我選擇的標題和正文。無論我點擊通知,它都會返回正確的categoryIdentifier,但帶有錯誤的actionIdentifier! – user2116499