与一个通知互动后,所有通知均已清除

时间:2018-06-26 22:51:36

标签: ios swift uilocalnotification

我的应用程序的预期行为是允许多个堆叠的应用程序,并且用户应该能够与每个通知进行分立的交互,但是当用户清除一个通知或使用通知中的所有我的应用程序中的某个操作按钮时,通知将从锁定屏幕中删除。在其中一个与之交互之后,是否有办法不清除所有通知?

通知创建者功能:

func sendNotification (customerName:String) {
    let durationString = String(durationDetected!)
    let content = UNMutableNotificationContent()
    content.title = "Duration: \(durationString)"
    content.subtitle = "Customer Name: \(customerName)"
    //content.body = "Don't forget to bring coffee."
    //content.badge = 1

     let customerFacing = UNNotificationAction(identifier:"yes",
                                            title:"Customer Facing",options:[])
    let notCustomerFacing = UNNotificationAction(identifier:
        "no", title: "Not Customer Facing", options: [])

    /*let changeTime = UNTextInputNotificationAction(identifier:
        "changeTime", title: "Modify Time", options: [])

    let changeName = UNTextInputNotificationAction(identifier:
        "changeName", title: "Modify Customer Name", options: [])*/


    let category = UNNotificationCategory(identifier: "actionCategory",
                                          actions: [customerFacing, notCustomerFacing],
                                          intentIdentifiers: [], options: [])

    content.categoryIdentifier = "actionCategory"


    UNUserNotificationCenter.current().setNotificationCategories(
        [category])

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3,
                                                    repeats: false)

    let eventArrayIndex = LocationService.locationInstance.eventLogArray.count - 1
    let requestIdentifier = String(eventArrayIndex)
    let request = UNNotificationRequest(identifier: requestIdentifier,
                                        content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request,
                                           withCompletionHandler: { (error) in
                                            // Handle error
    })
}

响应委托人:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive
response: UNNotificationResponse, withCompletionHandler completionHandler:
@escaping () -> Void) {

    let eventIndexString = response.notification.request.identifier
        eventIndex = Int(eventIndexString)!
        print("Index \(eventIndex)")

    switch response.actionIdentifier {
    case "yes":
            print("Received")
            LocationService.locationInstance.eventLogArray[eventIndex].status = "customerFacing"
            let currentID = LocationService.locationInstance.eventLogArray[eventIndex].uniqueID
            for (index,element) in LocationService.locationInstance.missedEventArray.enumerated() {
                if element.uniqueID == currentID {LocationService.locationInstance.missedEventArray.remove(at: index)}
            }
            self.commitEventToDatabase(eventObject: LocationService.locationInstance.eventLogArray[eventIndex])
            //self.eventLog.reloadData()
    case "no":
            print(LocationService.locationInstance.eventLogArray.count)
            print("WHHHAT")
            let currentID = LocationService.locationInstance.eventLogArray[eventIndex].uniqueID
            for (index,element) in LocationService.locationInstance.missedEventArray.enumerated() {
                if element.uniqueID == currentID {LocationService.locationInstance.missedEventArray.remove(at: index)}
            }
            LocationService.locationInstance.eventLogArray[eventIndex].status = "notCustomerFacing"
            print(LocationService.locationInstance.eventLogArray.count)
            //self.eventLog.reloadData()
    default:
        break
    }
    completionHandler()
}

0 个答案:

没有答案