I am running network task in background mode. when app is in foreground on physical phone. when click on simulate background fetch from debug menu ion Xcode, app disappears and network task (configured in application performFetchWithCompletionHandler) successfully completes. Which generates local notification, upon click on local notification works.
When same process is repeated when app is already in background. On completion of network task, notification appears and when I click (touch) on notification. App crash with following message
2019-01-09 16:47:37.711639+0500 tesapp[7284:2106715] *** Assertion failure in -[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] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'this request has been neutered - you can't call -sendResponse: twice nor after encoding it'
*** First throw call stack: (0x1b55f8ec4 0x1b47c9a40 0x1b550eb3c 0x1b5ffd1d0 0x1b7e60e48 0x105f14dc8 0x105f2382c 0x1b7e15088 0x1e23705fc 0x1e27a6810 0x105f13824 0x105f14dc8 0x105f22a78 0x1b5588df4 0x1b5583cbc 0x1b55831f0 0x1b77fc584 0x1e278ed40 0x1047e6430 0x1b5042bb4)
libc++abi.dylib: terminating with uncaught exception of type NSException
notification were registered in app delegate
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (granted, error) in
if granted {
print("Permission Granted")
} else {
print("Permission Denied")
}
})
presenting notifications
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])
}
displaying notifications
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()
}