diff --git a/dimensionhub/dimensionhubApp.swift b/dimensionhub/dimensionhubApp.swift index aec9bf4..6c6a66e 100644 --- a/dimensionhub/dimensionhubApp.swift +++ b/dimensionhub/dimensionhubApp.swift @@ -51,7 +51,7 @@ struct dimensionhubApp: App { } -class AppDelegate: UIResponder, UIApplicationDelegate { +class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { registerForPushNotifications() @@ -123,6 +123,41 @@ class AppDelegate: UIResponder, UIApplicationDelegate { completionHandler(.newData) } + // MARK: UNUserNotificationCenterDelegate + + // App 在前台时收到通知 + func userNotificationCenter(_ center: UNUserNotificationCenter, + willPresent notification: UNNotification, + withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { + + let userInfo = notification.request.content.userInfo + // 处理通知数据 + handleRemoteNotification(userInfo: userInfo) + + // 设置如何显示通知 + completionHandler([.banner, .sound]) + } + + // 用户点击通知 + func userNotificationCenter(_ center: UNUserNotificationCenter, + didReceive response: UNNotificationResponse, + withCompletionHandler completionHandler: @escaping () -> Void) { + + let userInfo = response.notification.request.content.userInfo + + // 处理通知数据 + handleRemoteNotification(userInfo: userInfo) + + // 根据通知内容跳转到特定页面 + if let deepLink = userInfo["deepLink"] as? String { + handleDeepLink(deepLink) + } + + completionHandler() + } + + // MARK: 消息处理 + private func handleRemoteNotification(userInfo: [AnyHashable: Any]) { print("收到远程通知: \(userInfo)") @@ -139,41 +174,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } -} - -// 处理通知交互 -extension AppDelegate: UNUserNotificationCenterDelegate { - - // App 在前台时收到通知 - func userNotificationCenter(_ center: UNUserNotificationCenter, - willPresent notification: UNNotification, - withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { - let userInfo = notification.request.content.userInfo - - // 处理通知数据 - handleRemoteNotification(userInfo: userInfo) - - // 设置如何显示通知 - completionHandler([.banner, .sound]) - } - - // 用户点击通知 - func userNotificationCenter(_ center: UNUserNotificationCenter, - didReceive response: UNNotificationResponse, - withCompletionHandler completionHandler: @escaping () -> Void) { - let userInfo = response.notification.request.content.userInfo - - // 处理通知数据 - handleRemoteNotification(userInfo: userInfo) - - // 根据通知内容跳转到特定页面 - if let deepLink = userInfo["deepLink"] as? String { - handleDeepLink(deepLink) - } - - completionHandler() - } - private func handleDeepLink(_ link: String) { // 实现深度链接处理逻辑 print("处理深度链接: \(link)")