2021-10-06 17:51:52 -07:00
|
|
|
import Foundation
|
2021-10-06 22:24:45 -07:00
|
|
|
import SwiftUI
|
2024-06-03 02:17:55 -07:00
|
|
|
import OSLog
|
2021-10-06 22:24:45 -07:00
|
|
|
|
|
|
|
|
class LocalNotificationManager {
|
|
|
|
|
|
|
|
|
|
var notifications = [Notification]()
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-06 22:24:45 -07:00
|
|
|
// Step 1 Request Permissions for notifications
|
2021-11-29 15:59:06 -08:00
|
|
|
private func requestAuthorization() {
|
2021-10-06 22:24:45 -07:00
|
|
|
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
|
|
|
|
|
|
|
|
|
|
if granted == true && error == nil {
|
2023-08-21 10:24:17 -05:00
|
|
|
self.scheduleNotifications()
|
2021-10-06 22:24:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2023-08-21 10:24:17 -05:00
|
|
|
func schedule() {
|
2021-10-06 22:24:45 -07:00
|
|
|
UNUserNotificationCenter.current().getNotificationSettings { settings in
|
|
|
|
|
switch settings.authorizationStatus {
|
|
|
|
|
case .notDetermined:
|
2023-08-21 10:24:17 -05:00
|
|
|
self.requestAuthorization()
|
2021-10-06 22:24:45 -07:00
|
|
|
case .authorized, .provisional:
|
|
|
|
|
self.scheduleNotifications()
|
|
|
|
|
default:
|
|
|
|
|
break // Do nothing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-06 22:24:45 -07:00
|
|
|
// This function iterates over the Notification objects in the notifications array and schedules them for delivery in the future
|
2021-11-29 15:59:06 -08:00
|
|
|
private func scheduleNotifications() {
|
|
|
|
|
for notification in notifications {
|
2024-01-20 14:05:29 -08:00
|
|
|
let content = UNMutableNotificationContent()
|
|
|
|
|
content.subtitle = notification.subtitle
|
|
|
|
|
content.title = notification.title
|
|
|
|
|
content.body = notification.content
|
|
|
|
|
content.sound = .default
|
|
|
|
|
content.interruptionLevel = .timeSensitive
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2023-08-21 10:24:17 -05:00
|
|
|
if notification.target != nil {
|
|
|
|
|
content.userInfo["target"] = notification.target
|
|
|
|
|
}
|
2024-01-20 14:05:29 -08:00
|
|
|
if notification.path != nil {
|
|
|
|
|
content.userInfo["path"] = notification.path
|
|
|
|
|
}
|
2021-10-06 22:24:45 -07:00
|
|
|
|
|
|
|
|
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
|
|
|
|
|
let request = UNNotificationRequest(identifier: notification.id, content: content, trigger: trigger)
|
|
|
|
|
|
|
|
|
|
UNUserNotificationCenter.current().add(request) { error in
|
|
|
|
|
|
|
|
|
|
guard error == nil else { return }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-06 22:24:45 -07:00
|
|
|
// Check and debug what local notifications have been scheduled
|
2021-11-29 15:59:06 -08:00
|
|
|
func listScheduledNotifications() {
|
2021-10-06 22:24:45 -07:00
|
|
|
UNUserNotificationCenter.current().getPendingNotificationRequests { notifications in
|
|
|
|
|
|
|
|
|
|
for notification in notifications {
|
2024-06-23 16:11:02 -07:00
|
|
|
Logger.services.debug("\(notification, privacy: .public)")
|
2021-10-06 22:24:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-06 22:24:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Notification {
|
|
|
|
|
var id: String
|
|
|
|
|
var title: String
|
2021-10-11 07:28:28 -07:00
|
|
|
var subtitle: String
|
2021-10-07 07:37:33 -07:00
|
|
|
var content: String
|
2023-08-21 10:24:17 -05:00
|
|
|
var target: String?
|
2024-01-20 14:05:29 -08:00
|
|
|
var path: String?
|
2021-10-06 22:24:45 -07:00
|
|
|
}
|