93 lines
3.4 KiB
Swift
93 lines
3.4 KiB
Swift
//
|
|
// NotificationUtils.swift
|
|
// TheNoiseClock
|
|
//
|
|
// Created by Matt Bruce on 9/7/25.
|
|
//
|
|
|
|
import UserNotifications
|
|
import Foundation
|
|
|
|
/// Notification helper functions
|
|
enum NotificationUtils {
|
|
|
|
/// Request notification permissions
|
|
/// - Returns: True if permission granted
|
|
static func requestPermissions() async -> Bool {
|
|
do {
|
|
let granted = try await UNUserNotificationCenter.current().requestAuthorization(
|
|
options: [.alert, .sound, .badge]
|
|
)
|
|
return granted
|
|
} catch {
|
|
DebugLogger.log("Error requesting notification permissions: \(error)", category: .general)
|
|
return false
|
|
}
|
|
}
|
|
|
|
/// Create notification content for alarm
|
|
/// - Parameters:
|
|
/// - title: Notification title
|
|
/// - body: Notification body
|
|
/// - soundName: Sound name for notification
|
|
/// - Returns: Configured notification content
|
|
static func createAlarmContent(title: String, body: String, soundName: String) -> UNMutableNotificationContent {
|
|
let content = UNMutableNotificationContent()
|
|
content.title = title
|
|
content.body = body
|
|
|
|
if soundName == "default" {
|
|
content.sound = UNNotificationSound.default
|
|
DebugLogger.log("Using default notification sound", category: .settings)
|
|
} else {
|
|
// Use the sound name directly since sounds.json now references CAF files
|
|
content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: soundName))
|
|
DebugLogger.log("Using custom alarm sound: \(soundName)", category: .settings)
|
|
DebugLogger.log("Sound file should be in main bundle: \(soundName)", category: .settings)
|
|
}
|
|
|
|
return content
|
|
}
|
|
|
|
/// Create calendar trigger for alarm
|
|
/// - Parameter date: Date for alarm
|
|
/// - Returns: Calendar notification trigger
|
|
static func createCalendarTrigger(for date: Date) -> UNCalendarNotificationTrigger {
|
|
let components = Calendar.current.dateComponents([.hour, .minute], from: date)
|
|
return UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
|
|
}
|
|
|
|
/// Schedule notification
|
|
/// - Parameters:
|
|
/// - identifier: Unique identifier for notification
|
|
/// - content: Notification content
|
|
/// - trigger: Notification trigger
|
|
/// - Returns: True if scheduled successfully
|
|
static func scheduleNotification(
|
|
identifier: String,
|
|
content: UNMutableNotificationContent,
|
|
trigger: UNNotificationTrigger
|
|
) async -> Bool {
|
|
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
|
|
|
|
do {
|
|
try await UNUserNotificationCenter.current().add(request)
|
|
return true
|
|
} catch {
|
|
DebugLogger.log("Error scheduling notification: \(error)", category: .general)
|
|
return false
|
|
}
|
|
}
|
|
|
|
/// Remove notification by identifier
|
|
/// - Parameter identifier: Notification identifier to remove
|
|
static func removeNotification(identifier: String) {
|
|
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
|
|
}
|
|
|
|
/// Remove all pending notifications
|
|
static func removeAllNotifications() {
|
|
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
|
|
}
|
|
}
|