冷启动app时候的唤起
This commit is contained in:
parent
1f68300417
commit
4452e4e43b
@ -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,24 +20,25 @@ 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 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
path = NavigationPath()
|
path = NavigationPath()
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in
|
||||||
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:
|
||||||
()
|
()
|
||||||
|
|||||||
@ -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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user