diff --git a/nahbar/nahbar/AddTodoView.swift b/nahbar/nahbar/AddTodoView.swift index 5b215e5..7d281da 100644 --- a/nahbar/nahbar/AddTodoView.swift +++ b/nahbar/nahbar/AddTodoView.swift @@ -1,6 +1,9 @@ import SwiftUI import SwiftData import UserNotifications +import OSLog + +private let logger = Logger(subsystem: "nahbar", category: "TodoNotification") struct AddTodoView: View { @Environment(\.nahbarTheme) var theme @@ -168,12 +171,19 @@ struct AddTodoView: View { private func scheduleReminder(for todo: Todo) { let center = UNUserNotificationCenter.current() - center.requestAuthorization(options: [.alert, .sound]) { granted, _ in - guard granted else { return } + center.requestAuthorization(options: [.alert, .sound]) { granted, error in + if let error { + logger.error("Berechtigung-Fehler: \(error.localizedDescription)") + } + guard granted else { + logger.warning("Notification-Berechtigung abgelehnt – keine Todo-Erinnerung.") + return + } let content = UNMutableNotificationContent() content.title = person.firstName content.body = todo.title content.sound = .default + content.userInfo = ["todoID": todo.id.uuidString] let components = Calendar.current.dateComponents( [.year, .month, .day, .hour, .minute], from: reminderDate ) @@ -183,7 +193,13 @@ struct AddTodoView: View { content: content, trigger: trigger ) - center.add(request) + center.add(request) { error in + if let error { + logger.error("Todo-Erinnerung konnte nicht geplant werden: \(error.localizedDescription)") + } else { + logger.info("Todo-Erinnerung geplant: \(todo.id.uuidString)") + } + } } } } diff --git a/nahbar/nahbar/NahbarApp.swift b/nahbar/nahbar/NahbarApp.swift index dc41f22..e10f38f 100644 --- a/nahbar/nahbar/NahbarApp.swift +++ b/nahbar/nahbar/NahbarApp.swift @@ -1,12 +1,39 @@ import SwiftUI import SwiftData import OSLog +import UserNotifications +import UIKit private let logger = Logger(subsystem: "nahbar", category: "App") +private let notificationLogger = Logger(subsystem: "nahbar", category: "Notification") + +// MARK: - App Delegate +// Setzt den UNUserNotificationCenterDelegate damit Benachrichtigungen auch im +// Vordergrund als Banner angezeigt werden. +final class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate { + func application( + _ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil + ) -> Bool { + UNUserNotificationCenter.current().delegate = self + return true + } + + /// Zeigt Benachrichtigungen auch an, wenn die App im Vordergrund läuft. + func userNotificationCenter( + _ center: UNUserNotificationCenter, + willPresent notification: UNNotification, + withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void + ) { + completionHandler([.banner, .sound]) + } +} @main struct NahbarApp: App { + @UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate + // Static let stellt sicher, dass der Container exakt einmal erstellt wird – // unabhängig davon, wie oft body ausgewertet wird. private static let containerBuild = AppGroup.makeMainContainerWithMigration()