48 lines
1.6 KiB
Swift
48 lines
1.6 KiB
Swift
//
|
|
// NotificationService+Extension.swift
|
|
// EQNNotificationService
|
|
//
|
|
// Created by Andrea Busi on 28/05/22.
|
|
// Copyright © 2022 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UserNotifications
|
|
|
|
|
|
extension NotificationService {
|
|
|
|
@objc(removeNotificationsForType:completion:)
|
|
func removeNotifications(
|
|
for type: String?,
|
|
completion: @escaping() -> Void
|
|
) {
|
|
guard let type = type else {
|
|
completion()
|
|
return
|
|
}
|
|
|
|
let notificationCenter = UNUserNotificationCenter.current()
|
|
|
|
notificationCenter.getDeliveredNotifications { notifications in
|
|
let sameTypeNotifications = notifications.filter { notification in
|
|
let payload = notification.request.content.userInfo
|
|
if let notificationType = payload["type"] as? String {
|
|
return notificationType == type
|
|
}
|
|
return false
|
|
}
|
|
|
|
let identifiers = sameTypeNotifications.map { $0.request.identifier }
|
|
notificationCenter.removeDeliveredNotifications(withIdentifiers: identifiers)
|
|
|
|
// !! Note: this is a known issue/bug
|
|
// we need to add a delay before invoking the completion, otherwise the notification will not be remved
|
|
// ref: https://stackoverflow.com/questions/53697279/why-are-notifications-not-removed-with-removedeliverednotifications
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
completion()
|
|
}
|
|
}
|
|
}
|
|
}
|