From 17f4dbd3ab981df6079ff9e375fd54e11ac73015 Mon Sep 17 00:00:00 2001 From: Sven Date: Wed, 22 Apr 2026 18:17:57 +0200 Subject: [PATCH] =?UTF-8?q?Fix:=20Push-Benachrichtigungen=20f=C3=BCr=20Tod?= =?UTF-8?q?o-Erinnerungen=20im=20Vordergrund?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - AppDelegate mit UNUserNotificationCenterDelegate: Notifications werden jetzt auch angezeigt wenn die App im Vordergrund läuft (.banner + .sound) - scheduleReminder: Fehler-Logging bei abgelehnter Berechtigung und center.add-Fehler hinzugefügt (analog zu AftermathNotificationManager) - userInfo mit todoID in Notification-Content aufgenommen Co-Authored-By: Claude Sonnet 4.6 --- nahbar/nahbar/AddTodoView.swift | 22 +++++++++++++++++++--- nahbar/nahbar/NahbarApp.swift | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) 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()