Files

45 lines
2.0 KiB
Swift

import UIKit
import UserNotifications
final class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Task { await PushRegistration.shared.subscribe(deviceToken: deviceToken) }
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
// Silent: push is best-effort. The user still gets in-app notifications.
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
// Called when the user taps a notification (foreground or background).
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let targetURLString = userInfo["target_url"] as? String,
let relayRaw = UserDefaults.standard.string(forKey: "relayURL"),
let relayURL = URL(string: relayRaw) {
PushRouter.shared.pendingRoute = ThreadRoute(postURL: targetURLString, relayURL: relayURL)
}
completionHandler()
}
// Show notification banner even when the app is in the foreground.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.banner, .sound])
}
}