fix scheduling notifications

This commit is contained in:
Trey t
2022-01-23 17:57:13 -06:00
parent 94bfe29022
commit 8cff6843cc

View File

@@ -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 {