43 lines
1.2 KiB
Swift
43 lines
1.2 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)
|
|
|
|
completion()
|
|
}
|
|
}
|
|
}
|