Fix: Push-Benachrichtigungen für Todo-Erinnerungen im Vordergrund
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user