冷启动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 SwiftUI
//
final class AppNavigation: ObservableObject {
static var shared = AppNavigation()
@ -19,12 +20,13 @@ 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 {
@ -36,7 +38,7 @@ final class AppNavigation: ObservableObject {
switch target {
case "detail":
if let dramaId = params["drama_id"] as? Int {
path.append(Destination.detail(id: dramaId))
self.targetDetailId = dramaId
}
default:
()

View File

@ -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 {