From 9708a00106422d6bc8c071637ad61077c2d91204 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 7 Apr 2025 11:52:37 +0800 Subject: [PATCH] fix notice --- dimensionhub/dimensionhubApp.swift | 49 +++++++++++++++++++----------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/dimensionhub/dimensionhubApp.swift b/dimensionhub/dimensionhubApp.swift index 9e2002e..2cb8136 100644 --- a/dimensionhub/dimensionhubApp.swift +++ b/dimensionhub/dimensionhubApp.swift @@ -7,10 +7,12 @@ import SwiftUI import SwiftData +import Observation @main struct dimensionhubApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate + @State var appNav = AppNavigation.shared var sharedModelContainer: ModelContainer = { let schema = Schema([ @@ -37,11 +39,17 @@ struct dimensionhubApp: App { UserDefaults.standard.set(Utils.defaultUserId(), forKey: "userId") } } - + var body: some Scene { WindowGroup { - NavigationStack { + NavigationStack(path: $appNav.path) { IndexView() + .navigationDestination(for: AppNavigation.Destination.self) { dest in + switch dest { + case .detail(id: let id): + DetailView(id: id) + } + } } .navigationViewStyle(.stack) .tint(.black) @@ -130,9 +138,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { - let userInfo = notification.request.content.userInfo + //let userInfo = notification.request.content.userInfo // 处理通知数据 - handleRemoteNotification(userInfo: userInfo) + //handleRemoteNotification(userInfo: userInfo) // 设置如何显示通知 completionHandler([.banner, .sound]) @@ -145,9 +153,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD let userInfo = response.notification.request.content.userInfo - // 处理通知数据 handleRemoteNotification(userInfo: userInfo) - // 根据通知内容跳转到特定页面 if let deepLink = userInfo["deepLink"] as? String { handleDeepLink(deepLink) @@ -159,18 +165,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD // MARK: 消息处理 private func handleRemoteNotification(userInfo: [AnyHashable: Any]) { - print("收到远程通知: \(userInfo)") - - // 解析通知内容 - if let aps = userInfo["aps"] as? [String: AnyObject] { - if let alert = aps["alert"] as? String { - print("通知消息: \(alert)") - } - - // 处理自定义数据 - if let customData = userInfo["customData"] as? [String: AnyObject] { - print("自定义数据: \(customData)") - } + if let customData = userInfo["custom_data"] as? [String: AnyObject], + let dramaId = customData["drama_id"] as? Int { + AppNavigation.shared.path.append(AppNavigation.Destination.detail(id: dramaId)) } } @@ -180,3 +177,19 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD } } + + +@Observable +final class AppNavigation { + static var shared = AppNavigation() + + enum Destination: Hashable { + case detail(id: Int) + } + + private init() { + + } + + var path = NavigationPath() +}