Действие из локального уведомления не работает в iOS 13 - PullRequest
0 голосов
/ 12 февраля 2020

Я использую локальное уведомление с использованием инфраструктуры UserNotifications, в которой у меня есть две кнопки действий. Теперь я хочу выполнить одну задачу при выборе этой кнопки действия. Мой вопрос: нужно ли мне запускать эту задачу в фоновом режиме? Поскольку я отправляю это локальное уведомление, основанное на срабатывании геозоны, когда мое приложение даже не работает и находится в нерабочем состоянии.

Настройка Центра уведомлений пользователей

        localCenter.delegate = self
        localCenter.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            if granted {
                // Define Actions
                let actionOpen = UNNotificationAction(identifier: GeoNotification.Action.open, title: "Open", options:.authenticationRequired)
                let actionCancel = UNNotificationAction(identifier: GeoNotification.Action.cancel, title: "Cancel", options: [.destructive])

                // Define Category
                let openCategory = UNNotificationCategory(identifier: GeoNotification.Category.OpenCategory, actions: [actionOpen, actionCancel], intentIdentifiers: [], options: [])
                let closeCategory = UNNotificationCategory(identifier: GeoNotification.Category.Closecategory, actions: [actionClose, actionCancel], intentIdentifiers: [], options: [])

                // Register Category
                UNUserNotificationCenter.current().setNotificationCategories([openCategory,closeCategory])
            }

Планирование локального уведомления

        let content = UNMutableNotificationContent()
        content.title = "GeoTrigger Event"
        content.body = isOpen ? "Open it" : "Close it"
        content.userInfo = ["isOpen": isOpen]
        content.sound = UNNotificationSound.default
        content.categoryIdentifier = category

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        localCenter.add(request)

Обработчик уведомлений

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

        print(response.notification.request.content.userInfo)
        let userInfo = response.notification.request.content.userInfo
        let openStatus = userInfo["isOpen"] as! Bool
        print("openStatus",openStatus)

        // Perform the task associated with the action.
        switch response.actionIdentifier {
        case GeoNotification.Action.open:
            self.openCloseFromNotification(isOpen: true) //This function is not working..
            break
        case GeoNotification.Action.close:
            self.openCloseFromNotification(isOpen: false) //This function is not working..
            break
        case GeoNotification.Action.cancel:
            break
        default:
            break
        }
        // Always call the completion handler when done.
        completionHandler()
    }

...