Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MehdiChennoufi/209844816e37320ec5fef747cad275c6 to your computer and use it in GitHub Desktop.
Save MehdiChennoufi/209844816e37320ec5fef747cad275c6 to your computer and use it in GitHub Desktop.
Exntension to `UNNotification` for "snoozing" a notification.
//
// UNNotification+Reschedule.swift
// Meal Plan
//
// Created by Simon Ljungberg on 17/11/16.
// License: MIT
//
import UIKit
import UserNotifications
extension UNNotification {
func snoozeNotification(for hours: Int, minutes: Int) {
let content = self.request.content
let identifier = self.request.identifier
guard let oldTrigger = self.request.trigger as? UNCalendarNotificationTrigger else {
fatalError("Cannot reschedule notification without calendar trigger.")
}
var components = oldTrigger.dateComponents
components.hour = (components.hour ?? 0) + hours
components.minute = (components.minute ?? 0) + minutes
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Reschduling failed", error.localizedDescription)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment