diff --git a/Shared/LocalNotification.swift b/Shared/LocalNotification.swift index c05177e..65f2151 100644 --- a/Shared/LocalNotification.swift +++ b/Shared/LocalNotification.swift @@ -29,29 +29,37 @@ class LocalNotification { } public class func scheduleReminder(atTime time: Date, withTitle title: String) { - let _ = LocalNotification.createNotificationCategory() - - let notificationContent = UNMutableNotificationContent() - notificationContent.title = title - - notificationContent.badge = NSNumber(value: 1) - notificationContent.sound = .default - notificationContent.categoryIdentifier = LocalNotification.categoryName - - let calendar = Calendar.current - let time = calendar.dateComponents([.hour,.minute], from: time) - - var datComp = DateComponents() - datComp.hour = time.hour - datComp.minute = time.minute - - let trigger = UNCalendarNotificationTrigger(dateMatching: datComp, repeats: true) - let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: trigger) - UNUserNotificationCenter.current().add(request) { (error : Error?) in - if let theError = error { - print(theError.localizedDescription) + LocalNotification.testIfEnabled(completion: { result in + switch result{ + case .success(_): + let _ = LocalNotification.createNotificationCategory() + + let notificationContent = UNMutableNotificationContent() + notificationContent.title = title + + notificationContent.badge = NSNumber(value: 1) + notificationContent.sound = .default + notificationContent.categoryIdentifier = LocalNotification.categoryName + + let calendar = Calendar.current + let time = calendar.dateComponents([.hour,.minute], from: time) + + var datComp = DateComponents() + datComp.hour = time.hour + datComp.minute = time.minute + + let trigger = UNCalendarNotificationTrigger(dateMatching: datComp, repeats: true) + let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: trigger) + UNUserNotificationCenter.current().add(request) { (error : Error?) in + if let theError = error { + print(theError.localizedDescription) + } + } + case .failure(_): + // Todo: show enable this + break } - } + }) } private class func createNotificationCategory() -> UNNotificationCategory {