Проблема с Notifications Service Extension


#1

Пытаюсь реализовать удаление старых уведомлений из Notification Center, но при попытке получить все отображаемые уведомления, возвращается пустой список, хотя уведомления висят.

Использую Swift 3.2, Xcode 9.2, iPhone 6, iOS 11.2.6
Мой код:

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    self.contentHandler = contentHandler
    bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

    var count: Int = 0
    if let bestAttemptContent = bestAttemptContent {
        var customID: String?
        var matching: UNNotification?
        let userInfo = request.content.userInfo
        var notificationType: NotificationType
        
        bestAttemptContent.title = "\(request.identifier)"
        bestAttemptContent.body = "Nothing"
        
        if let data = userInfo["data"] as? [String : Any] {
            bestAttemptContent.body = "data found"
            if let t = data["type"] as? Int {
                bestAttemptContent.body = bestAttemptContent.body + "\ndata -> type found"
                notificationType = NotificationType(rawValue: t) ?? .other
            }
            else {
                notificationType = .other
            }
            
            if let key = getId(for: notificationType) {
                customID = data[key] as? String
                bestAttemptContent.body = bestAttemptContent.body + "\nkey found - \(key)"
                bestAttemptContent.body = bestAttemptContent.body + "\ncustom id - \(customID ?? "null")"
                UNUserNotificationCenter.current()
                    .getDeliveredNotifications { notifications in
                        count = notifications.count
                        matching = notifications.first(where: { notify in
                            bestAttemptContent.body = bestAttemptContent.body + "\ncontent found"
                            let existingUserInfo = notify.request.content.userInfo
                            if let data = existingUserInfo["data"] as? [String : Any] {
                                bestAttemptContent.body = bestAttemptContent.body + "\ndata2 found"
                                let id = data[key] as? String
                                bestAttemptContent.body = bestAttemptContent.body + "\nnotification id - \(id ?? "null")"
                                return id == customID
                            }
                                
                            return false
                        })
                }
                bestAttemptContent.body = bestAttemptContent.body + "\nnotifications count - \(count)"
                
                if let matchExists = matching {
                    UNUserNotificationCenter.current().removeDeliveredNotifications(
                        withIdentifiers: [matchExists.request.identifier]
                    )
                }
                else {
                    bestAttemptContent.body = bestAttemptContent.body + "\nmatch doesn't exists"
                }
            }
        }
        
        contentHandler(bestAttemptContent)
    }
}

Ключ и значение из нового уведомления достает верно, код валидный, но список текущий уведомлений почему-то пуст. На SO в одной теме писали, что возможно баг с версией iOS 11.2. Возможности проверить на другой версии iOS в данный момент нет.

Может кто знает чего?


#2

Если кому будет интересно, то проверку на matching нужно делать внутри callback’a .getDeliveredNotifications, после того как мы получили matching.


Парсинг push notifications