UNNotificationCenterDelegate didReceive функция не вызывается при действии - PullRequest
0 голосов
/ 13 февраля 2019

У меня есть намерение, что при вызове извлекает объекты из базы данных Realm и запускает уведомление с действиями, соответствующими названию этих объектов.

Когда пользователь выбирает одно из действий, метод в ответе 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?

...