iOS10:点击本地通知中的操作不会将应用带到前台

iOS10: tapping an action from local notification does not bring app to the foreground

我正在尝试执行通知中的操作。到目前为止,我能够触发正确的委托功能,但在点击应用程序后不会将其带到前台。

相关代码:

@available(iOS 10.0, *)
func registerCategory() -> Void{
    print("register category")
    let callNow = UNNotificationAction(identifier: "call", title: "Call now", options: [])
    let clear = UNNotificationAction(identifier: "clear", title: "Clear", options: [])
    let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "IDENT123", actions: [callNow, clear], intentIdentifiers: [], options: [])

    let center = UNUserNotificationCenter.currentNotificationCenter()
    center.setNotificationCategories([category])
}

@available(iOS 10.0, *)
func scheduleNotification(event : String, interval: NSTimeInterval) {

    print("schedule ", event)

    let content = UNMutableNotificationContent()

    content.title = event
    content.body = "body"
    content.categoryIdentifier = "CALLINNOTIFICATION"
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false)
    let identifier = "id_"+event
    let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)

    let center = UNUserNotificationCenter.currentNotificationCenter()
    center.addNotificationRequest(request) { (error) in

    }
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {

    print("willPresent")
    completionHandler([.Badge, .Alert, .Sound])
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {

    let notification: UNNotification = response.notification
    let UUID = notification.request.content.userInfo["UUID"] as! String

    switch (response.actionIdentifier) {
    case "COMPLETE":

        UNUserNotificationCenter.currentNotificationCenter().removeDeliveredNotificationsWithIdentifiers([UUID])

    case "CALLIN":

        let call = Call()

        CalendarController.sharedInstance.fetchMeetingByUUID(UUID, completion: { (thisMeeting) -> Void in
            if(!CallIn.Yield(thisMeeting).ConferenceCallNumber.containsString("None")){
                call._call(thisMeeting)
            }else{
                //will open detail view, in case that no number were detected
                NSNotificationCenter.defaultCenter().postNotificationName("OpenDetailViewOfMeeting", object: self, userInfo: ["UUID":UUID])
            }
        })
        UNUserNotificationCenter.currentNotificationCenter().removeDeliveredNotificationsWithIdentifiers([UUID])
    default: // switch statements must be exhaustive - this condition should never be met
        log.error("Error: unexpected notification action identifier: \(UUID)")
    }

    completionHandler()
}

我能够使用断点命中委托函数 didReceiveNotificationResponse(),它执行我放置在那里的一些操作,但不是以预期的方式(它必须启动设备调用,而不是它只是关闭通知列表,没有任何反应,但是当我再次手动打开应用程序时,调用开始,就好像没有权限从通知中打开应用程序一样)。

我自己找到了原因,所以这可能对以后的人有帮助。答案很简单。在创建通知的动作时,有这个参数:options。当你注册类别时,你需要像这样把它放在 .Foreground 或 .Destructive 中:

func reisterCategory () {
    let callNow = UNNotificationAction(identifier: NotificationActions.callNow.rawValue, title: "Call now", options: UNNotificationActionOptions.Foreground)
    let clear = UNNotificationAction(identifier: NotificationActions.clear.rawValue, title: "Clear", options: UNNotificationActionOptions.Destructive)
    let category = UNNotificationCategory.init(identifier: "NOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: [])
    let center = UNUserNotificationCenter.currentNotificationCenter()
    center.setNotificationCategories([category])
}