-
-
Save MehdiChennoufi/209844816e37320ec5fef747cad275c6 to your computer and use it in GitHub Desktop.
Exntension to `UNNotification` for "snoozing" a notification.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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