冷启动app时候的唤起
This commit is contained in:
parent
1f68300417
commit
4452e4e43b
@ -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:
|
||||
()
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user