From cfb186e0ea17dbf899f6e8f5f9fda35ff9639659 Mon Sep 17 00:00:00 2001 From: Jungpyo Hong Date: Tue, 22 Jun 2021 10:07:06 -0500 Subject: [PATCH] Add files via upload --- .../LocalNotificationManager.swift | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Helper/LocalNotificationHelper/LocalNotificationManager.swift diff --git a/Helper/LocalNotificationHelper/LocalNotificationManager.swift b/Helper/LocalNotificationHelper/LocalNotificationManager.swift new file mode 100644 index 0000000..1c42dd5 --- /dev/null +++ b/Helper/LocalNotificationHelper/LocalNotificationManager.swift @@ -0,0 +1,89 @@ +// +// LocalNotificationManager.swift +// Local_Notification +// +// Created by JungpyoHong on 6/21/21. +// + +import Foundation +import UserNotifications +import UIKit + +struct LocalNotification { + var id: String + var title: String + var body: String +} + +enum LocalNotificationDurationType { + case days + case hours + case minutes + case seconds +} + +struct LocalNotificationManager { + + static private var notifications = [LocalNotification]() + + static private func requestPermission() -> Void { + UNUserNotificationCenter + .current() + .requestAuthorization(options: [.alert, .badge, .alert]) { granted, error in + if granted == true && error == nil { + // Permission accept + } + } + } + + static private func addNotification(title: String, body: String) -> Void { + notifications.append(LocalNotification(id: UUID().uuidString, title: title, body: body)) + } + + static private func scheduleNotifications(_ durationInSeconds: Int, repeats: Bool, userInfo: [AnyHashable : Any]) { + UIApplication.shared.applicationIconBadgeNumber = 0 + for notification in notifications { + let content = UNMutableNotificationContent() + content.title = notification.title + content.body = notification.body + content.sound = UNNotificationSound.default + content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + 1) + content.userInfo = userInfo + + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(durationInSeconds), repeats: repeats) + let request = UNNotificationRequest(identifier: notification.id, content: content, trigger: trigger) + + UNUserNotificationCenter.current().removeAllPendingNotificationRequests() + UNUserNotificationCenter.current().add(request) { error in + guard error == nil else { return } + print("Scheduling notification with id: \(notification.id)") + } + } + notifications.removeAll() + } + + static private func scheduleNotifications(_ duration: Int, of type: LocalNotificationDurationType, repeats: Bool, userInfo: [AnyHashable : Any]) { + var seconds = 0 + switch type { + case .seconds: + seconds = duration + case .minutes: + seconds = duration * 60 + case .hours: + seconds = duration * 60 * 60 + case .days: + seconds = duration * 60 * 60 * 24 + } + scheduleNotifications(seconds, repeats: repeats, userInfo: userInfo) + } + + static func cancel() { + UNUserNotificationCenter.current().removeAllPendingNotificationRequests() + } + + static func setNotification(_ duration: Int, of type: LocalNotificationDurationType, repeats: Bool, title: String, body: String, userInfo: [AnyHashable : Any]) { + requestPermission() + addNotification(title: title, body: body) + scheduleNotifications(duration, of: type, repeats: repeats, userInfo: userInfo) + } +}