2

我在后台模式下运行网络任务。当应用程序在物理手机上处于前台时。当单击从调试菜单 ion Xcode 模拟后台提取时,应用程序消失并且网络任务(在应用程序 performFetchWithCompletionHandler 中配置)成功完成。单击本地通知后会生成本地通知。

当应用程序已经在后台时重复相同的过程。网络任务完成后,会出现通知,当我单击(触摸)通知时。应用程序崩溃并显示以下消息

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:两次也不编码后'

*** 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:以 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()
}
4

0 回答 0