// // LocalNotification.swift // Feels // // Created by Trey Tartt on 1/8/22. // import Foundation import UserNotifications class LocalNotification { public enum ActionType: String { case horrible = "HORRIBLE_ACTION" case bad = "BAD_ACTION" case average = "AVERAGE_ACTION" case good = "GOOD_ACTION" case great = "GREAT_ACTION" } static let categoryName = "MOOD_UPDATE" public class func testIfEnabled(completion: @escaping (Result) -> Void) { UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in if success { completion(.success(true)) } else if let error = error { completion(.failure(error)) } } } public class func scheduleReminder(atTime time: Date, withTitle title: String) { 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 { let moodCategory = UNNotificationCategory(identifier: LocalNotification.categoryName, actions: [horribleAction, badAction, averageAction, goodAction, greatAction], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "", options: .customDismissAction) // Register the notification type. let notificationCenter = UNUserNotificationCenter.current() notificationCenter.setNotificationCategories([moodCategory]) return moodCategory } private class var horribleAction: UNNotificationAction { let acceptAction = UNNotificationAction(identifier: ActionType.horrible.rawValue, title: "Horrible", options: []) return acceptAction } private class var badAction: UNNotificationAction { let acceptAction = UNNotificationAction(identifier: ActionType.bad.rawValue, title: "Bad", options: []) return acceptAction } private class var averageAction: UNNotificationAction { let acceptAction = UNNotificationAction(identifier: ActionType.average.rawValue, title: "Average", options: []) return acceptAction } private class var goodAction: UNNotificationAction { let acceptAction = UNNotificationAction(identifier: ActionType.good.rawValue, title: "Good", options: []) return acceptAction } private class var greatAction: UNNotificationAction { let acceptAction = UNNotificationAction(identifier: ActionType.great.rawValue, title: "Great", options: []) return acceptAction } public class func removeNotificaiton() { UNUserNotificationCenter.current().removeAllPendingNotificationRequests() } }