Is there any way to remove all remote notifications that are one day old?

82 views Asked by At

Is there any way to remove all remote notifications that are one day old?

1

There are 1 answers

0
nRewik On

Try this,

UNUserNotificationCenter.current().getDeliveredNotifications { notifications in

    let yesterday = NSCalendar.current.date(byAdding: .day, value: -1, to: Date())!

    let identifiersToRemove = notifications
        .filter { $0.date.compare(yesterday) == .orderedAscending }
        .map { $0.request.identifier }

    UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiersToRemove)
}