我有一个自定义的意图,当被调用时,它会从Realm数据库中检索对象,并使用与那些对象的标题相对应的Actions触发通知。
当用户选择一项操作时,didReceive响应中的方法应从Realm数据库中删除项目。
在Siri触发的自定义意图处理程序中
let content = UNMutableNotificationContent()
content.title = "Select note to mark as completed"
content.body = "Swipe down to see which tasks you have to complete"
content.categoryIdentifier = "completeNoteCategory"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
//Fetch items from Realm, create Notification Actions for each item
var actionsArray = [UNNotificationAction]()
ResultsFetcher.fetchNotes { (notes) in
notes.forEach({ (note) in
actionsArray.append(UNNotificationAction(identifier: note.id, title: note.content, options: []))
})
}
//Register notification category
let requestIdentifier = "completeNoteNotification"
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
let category = UNNotificationCategory(identifier: "completeNoteCategory", actions: actionsArray, intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
UNUserNotificationCenter.current().add(request) { (error) in
print("error")
}
completion(MarkNoteAsCompleteIntentResponse(code: .success, userActivity: nil))
}
在appdelegate中
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
ResultsFetcher.deleteNote(withId: response.actionIdentifier)
completionHandler()
}
测试deleteNote方法表明它在应用程序中有效,但是由于某些原因,当应用程序处于前台时选择该动作时未调用该方法。我需要在appdelegate中的某个地方再次声明类别吗?