From 4452e4e43b4ffec80295f9f9d4dc0565a6d30707 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 1 Jul 2025 16:07:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=B7=E5=90=AF=E5=8A=A8app=E6=97=B6?= =?UTF-8?q?=E5=80=99=E7=9A=84=E5=94=A4=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dimensionhub/Core/AppNavigation.swift | 8 +++++--- dimensionhub/dimensionhubApp.swift | 27 ++++++++++++--------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/dimensionhub/Core/AppNavigation.swift b/dimensionhub/Core/AppNavigation.swift index 2159a77..81a7147 100644 --- a/dimensionhub/Core/AppNavigation.swift +++ b/dimensionhub/Core/AppNavigation.swift @@ -8,6 +8,7 @@ import Foundation import SwiftUI +// 应用的导航设置 final class AppNavigation: ObservableObject { static var shared = AppNavigation() @@ -19,24 +20,25 @@ final class AppNavigation: ObservableObject { @Published var path = NavigationPath() + @Published var targetDetailId: Int = 0 + func append(dest: Destination) { path.append(dest) } func handleNotification(_ userInfo: [AnyHashable: Any]) { - print(userInfo) guard let customData = userInfo["custom_data"] as? [String: AnyObject], let target = customData["target"] as? String, let params = customData["params"] as? [String : AnyObject] else { return } - + path = NavigationPath() DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in switch target { case "detail": if let dramaId = params["drama_id"] as? Int { - path.append(Destination.detail(id: dramaId)) + self.targetDetailId = dramaId } default: () diff --git a/dimensionhub/dimensionhubApp.swift b/dimensionhub/dimensionhubApp.swift index 0d4234b..3e1dd29 100644 --- a/dimensionhub/dimensionhubApp.swift +++ b/dimensionhub/dimensionhubApp.swift @@ -33,6 +33,9 @@ struct dimensionhubApp: App { }() init() { + // 尽可能早设置代理对象 + UNUserNotificationCenter.current().delegate = appDelegate + let userId = KeychainHelper.getPersistentUserId() print("user_id is: \(userId)") } @@ -51,10 +54,18 @@ struct dimensionhubApp: App { SearchView() } } - .environment(\.userId, KeychainHelper.getPersistentUserId()) + .onChange(of: appNav.targetDetailId) { _, dramaId in + if dramaId > 0 { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { + appNav.append(dest: .detail(id: dramaId)) + appNav.targetDetailId = 0 + } + } + } } .navigationViewStyle(.stack) .tint(.black) + .environment(\.userId, KeychainHelper.getPersistentUserId()) .environmentObject(appNav) .preferredColorScheme(.light) } @@ -69,12 +80,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD Task.detached { await self.registerForPushNotifications() } - return true } private func registerForPushNotifications() { - UNUserNotificationCenter.current().delegate = self // 请求通知权限 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {granted, error in NSLog("通知权限 granted: \(granted)") @@ -85,19 +94,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD // 获取通知设置 UNUserNotificationCenter.current().getNotificationSettings { settings in NSLog("通知设置: \(settings)") - guard settings.authorizationStatus == .authorized else { return } NSLog("通知设置: authorized!!!!") - DispatchQueue.main.async { // 注册远程通知 UIApplication.shared.registerForRemoteNotifications() } } - } } @@ -129,9 +135,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD // 收到远程通知(App 在前台) func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { - // 处理通知数据 - handleRemoteNotification(userInfo: userInfo) - completionHandler(.newData) } @@ -141,11 +144,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { - - let userInfo = notification.request.content.userInfo - // 处理通知数据 - handleRemoteNotification(userInfo: userInfo) - // 设置如何显示通知 completionHandler([.banner, .sound]) } @@ -156,7 +154,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo - handleRemoteNotification(userInfo: userInfo) // 根据通知内容跳转到特定页面 if let deepLink = userInfo["deepLink"] as? String {