Files
ABS-Client/ABS Client/Audiobookshelf swift/Services/NotificationManager.swift
Scarriffle 134482bb4a Enhance download notifications and settings; add support for live activities
- Updated notification body to use proper German quotation marks for download failure alerts.
- Refactored audio interruption handling in PlayerEngine to utilize MainActor.assumeIsolated for better performance.
- Introduced nonisolated properties for support and downloads directories in ProgressSyncManager.
- Improved FullHistoryView and MainView with additional toolbar items and alerts for download failures.
- Added new toggles in SettingsView for parallel downloads and mobile data usage.
- Created new color assets for the DownloadWidgetExtension and updated widget appearance.
- Implemented Info.plist for DownloadWidgetExtension to support widget functionality.
- Enabled live activities support in iOS Info.plist.
2026-06-08 19:32:54 +02:00

45 lines
1.8 KiB
Swift

import Foundation
import UserNotifications
#if canImport(UIKit)
import UIKit
#endif
/// Manages local UNUserNotificationCenter notifications for download events.
@MainActor
final class NotificationManager {
static let shared = NotificationManager()
private init() {}
/// Requests notification authorization the first time the app launches.
/// Skips the system prompt if permission has already been granted or denied.
func requestPermissionIfNeeded() async {
let center = UNUserNotificationCenter.current()
let settings = await center.notificationSettings()
guard settings.authorizationStatus == .notDetermined else { return }
_ = try? await center.requestAuthorization(options: [.alert, .sound])
}
/// Fires a local notification when a download fails.
/// Suppressed while the app is in the foreground an in-app alert already
/// surfaces the error there, so a notification would be redundant.
func sendDownloadFailedNotification(itemTitle: String) async {
#if canImport(UIKit)
guard UIApplication.shared.applicationState != .active else { return }
#endif
let content = UNMutableNotificationContent()
content.title = "Download fehlgeschlagen"
// \u{201E} = (DOUBLE LOW-9 QUOTATION MARK, German opening)
// \u{201C} = " (LEFT DOUBLE QUOTATION MARK, German closing)
content.body = "\u{201E}\(itemTitle)\u{201C} konnte nicht heruntergeladen werden."
content.sound = .default
let request = UNNotificationRequest(
identifier: "download-failed-\(UUID().uuidString)",
content: content,
trigger: nil // deliver immediately
)
try? await UNUserNotificationCenter.current().add(request)
}
}