Я запускаю сетевое задание в фоновом режиме. когда приложение находится на переднем плане на физическом телефоне. при нажатии на имитацию фоновой выборки из меню отладки в Xcode приложение исчезает и сетевое задание (настроенное в программе executeFetchWithCompletionHandler) успешно завершается. Который генерирует локальное уведомление, при нажатии на локальное уведомление работает.
Когда тот же процесс повторяется, когда приложение уже в фоновом режиме. По завершении сетевого задания появляется уведомление, и когда я нажимаю (касаюсь) уведомление. Сбой приложения со следующим сообщением
2019-01-09 16: 47: 37.711639 + 0500 tesapp [7284: 2106715] *** Ошибка подтверждения в - [UIFetchContentInBackgroundAction sendResponse: withCompletion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources /BaseBoard/BaseBoard-360.25/BaseBoard/BSAction.m:440
2019-01-09 16: 47: 37.712077 + 0500 tesapp [7284: 2106715] *** Завершение работы приложения из-за необработанного исключения «NSInternalInconsistencyException», причина: «этот запрос был кастрирован - вы не можете вызвать -sendResponse : дважды, ни после кодирования '
*** Первый стек вызовов вызовов:
(0x1b55f8ec4 0x1b47c9a40 0x1b550eb3c 0x1b5ffd1d0 0x1b7e60e48 0x105f14dc8 0x105f2382c 0x1b7e15088 0x1e23705fc 0x1e27a6810 0x105f13824 0x105f14dc8 0x105f22a78 0x1b5588df4 0x1b5583cbc 0x1b55831f0 0x1b77fc584 0x1e278ed40 0x1047e6430 0x1b5042bb4)
libc ++ abi.dylib: завершается с необработанным исключением типа NSException
уведомление было зарегистрировано в приложении делегат
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (granted, error) in
if granted {
print("Permission Granted")
} else {
print("Permission Denied")
}
})
представление уведомлений
func showNotification() {
let notification = UNMutableNotificationContent()
notification.badge = 1
notification.title = title
notification.subtitle = subtitle
notification.sound = UNNotificationSound.default
notification.categoryIdentifier = "category_notification"
notification.body = body
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: identifier, content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
// UNUserNotificationCenter.current().add(request, withCompletionHandler: {(error) in if error != nil { print("SOMETHING WENT WRONG") } })
let actionAccept = UNNotificationAction(identifier: "accept", title: "Accept", options: .foreground)
let actionReject = UNNotificationAction(identifier: "reject", title: "Reject", options: .destructive)
let actionComment = UNTextInputNotificationAction(identifier: "comment", title: "Add Comment", options: .authenticationRequired, textInputButtonTitle: "Send", textInputPlaceholder: "Add Comment Here")
let categoryNotification = UNNotificationCategory(identifier: "category_notification",actions: [actionAccept,actionReject,actionComment],intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([categoryNotification])
}
отображение уведомлений
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
os_log("willPresent %{public}@", log: log, notification)
completionHandler([.badge,.alert,.sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.notification.request.content.categoryIdentifier {
case "GENERAL": break
case "category_notification":
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["notification", "notificationComplete"])
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["notification", "notificationComplete"])
switch response.actionIdentifier {
case "accept": debugPrint("notification status: accepted")
case "reject": debugPrint("notification status: rejected")
case "comment": debugPrint("notification comments: \( (response as! UNTextInputNotificationResponse).userText ) ")
default: break
}
default: break
}
completionHandler()
}