Tengo un activador para mostrar una notificación al usuario:
let content = UNMutableNotificationContent() content.title = "Title" content.body = "Body" content.sound = UNNotificationSound.default let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 20, repeats: false) let request = UNNotificationRequest(identifier: "TestIdentifier", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)¿Hay alguna manera de cancelar ese activador una vez que se ha configurado?
¿Cómo cancelo la notificación antes de timeInterval el intervalo de tiempo y llamo a la notificación?
Puede cancelar o eliminar las notificaciones llamando al:
let center = UNUserNotificationCenter.current()Eliminar notificaciones pendientes con identificador dado
center.removePendingNotificationRequests(withIdentifiers: [“givenIdentifier”])Y elimine las notificaciones entregadas con el identificador dado
center.removeDeliveredNotifications(withIdentifiers: [“givenIdentifier”])Para construir sobre la respuesta de Mannopson:
Esto elimina todas las notificaciones pendientes ( Swift 5 )
func cancelNotifications() -> Void { UNUserNotificationCenter.current().removeAllPendingNotificationRequests() }