冷启动app时候的唤起

This commit is contained in:
anlicheng 2025-07-01 16:07:29 +08:00
parent 1f68300417
commit 4452e4e43b
2 changed files with 17 additions and 18 deletions

View File

@ -8,6 +8,7 @@
import Foundation import Foundation
import SwiftUI import SwiftUI
//
final class AppNavigation: ObservableObject { final class AppNavigation: ObservableObject {
static var shared = AppNavigation() static var shared = AppNavigation()
@ -19,12 +20,13 @@ final class AppNavigation: ObservableObject {
@Published var path = NavigationPath() @Published var path = NavigationPath()
@Published var targetDetailId: Int = 0
func append(dest: Destination) { func append(dest: Destination) {
path.append(dest) path.append(dest)
} }
func handleNotification(_ userInfo: [AnyHashable: Any]) { func handleNotification(_ userInfo: [AnyHashable: Any]) {
print(userInfo)
guard let customData = userInfo["custom_data"] as? [String: AnyObject], guard let customData = userInfo["custom_data"] as? [String: AnyObject],
let target = customData["target"] as? String, let target = customData["target"] as? String,
let params = customData["params"] as? [String : AnyObject] else { let params = customData["params"] as? [String : AnyObject] else {
@ -36,7 +38,7 @@ final class AppNavigation: ObservableObject {
switch target { switch target {
case "detail": case "detail":
if let dramaId = params["drama_id"] as? Int { if let dramaId = params["drama_id"] as? Int {
path.append(Destination.detail(id: dramaId)) self.targetDetailId = dramaId
} }
default: default:
() ()

View File

@ -33,6 +33,9 @@ struct dimensionhubApp: App {
}() }()
init() { init() {
//
UNUserNotificationCenter.current().delegate = appDelegate
let userId = KeychainHelper.getPersistentUserId() let userId = KeychainHelper.getPersistentUserId()
print("user_id is: \(userId)") print("user_id is: \(userId)")
} }
@ -51,10 +54,18 @@ struct dimensionhubApp: App {
SearchView() 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) .navigationViewStyle(.stack)
.tint(.black) .tint(.black)
.environment(\.userId, KeychainHelper.getPersistentUserId())
.environmentObject(appNav) .environmentObject(appNav)
.preferredColorScheme(.light) .preferredColorScheme(.light)
} }
@ -69,12 +80,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
Task.detached { Task.detached {
await self.registerForPushNotifications() await self.registerForPushNotifications()
} }
return true return true
} }
private func registerForPushNotifications() { private func registerForPushNotifications() {
UNUserNotificationCenter.current().delegate = self
// //
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {granted, error in UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {granted, error in
NSLog("通知权限 granted: \(granted)") NSLog("通知权限 granted: \(granted)")
@ -85,19 +94,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
// //
UNUserNotificationCenter.current().getNotificationSettings { settings in UNUserNotificationCenter.current().getNotificationSettings { settings in
NSLog("通知设置: \(settings)") NSLog("通知设置: \(settings)")
guard settings.authorizationStatus == .authorized else { guard settings.authorizationStatus == .authorized else {
return return
} }
NSLog("通知设置: authorized!!!!") NSLog("通知设置: authorized!!!!")
DispatchQueue.main.async { DispatchQueue.main.async {
// //
UIApplication.shared.registerForRemoteNotifications() UIApplication.shared.registerForRemoteNotifications()
} }
} }
} }
} }
@ -129,9 +135,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
// App // App
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//
handleRemoteNotification(userInfo: userInfo)
completionHandler(.newData) completionHandler(.newData)
} }
@ -141,11 +144,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
func userNotificationCenter(_ center: UNUserNotificationCenter, func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification, willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
//
handleRemoteNotification(userInfo: userInfo)
// //
completionHandler([.banner, .sound]) completionHandler([.banner, .sound])
} }
@ -156,7 +154,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
withCompletionHandler completionHandler: @escaping () -> Void) { withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo let userInfo = response.notification.request.content.userInfo
handleRemoteNotification(userInfo: userInfo) handleRemoteNotification(userInfo: userInfo)
// //
if let deepLink = userInfo["deepLink"] as? String { if let deepLink = userInfo["deepLink"] as? String {