首页 > 解决方案 > 如何在 SwiftUI 中打开通知点击的特定视图?

问题描述

一旦点击通知,我就会尝试将用户发送到 DetailView。我已经遵循了多个说明,例如这个。但是,当我对其进行测试时,点击通知仍然会将我带到错误的视图(settingsView)。

在 App Delegate 中,我目前有这个设置(下面的更新代码滚动)

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        registerBackgroundTasks()
        FirebaseApp.configure()
        UNUserNotificationCenter.current().delegate = self
    
        
                return true
    }

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        if response.actionIdentifier == "open" {
                NotificationCenter.default.post(name: NSNotification.Name("DetailViewNew"), object: nil)
            }
        
        completionHandler()
    }

在 SettingsDetailView 中(触发和设置通知的位置)

func configureNewAlert() {
    let center = UNUserNotificationCenter.current()
    
    center.removeAllDeliveredNotifications()
    center.removeAllPendingNotificationRequests()

        let content = UNMutableNotificationContent()
        content.title = "Header"
        content.body = "Test"
        content.sound = UNNotificationSound.default
    
    
    if let selectedHour = Calendar.current.dateComponents([.hour], from: userData.wakeUpTime).hour, let selectedMinute = Calendar.current.dateComponents([.minute], from: userData.wakeUpTime).minute{
        
            var dateComponents = DateComponents()
            dateComponents.hour = selectedHour
            dateComponents.minute = selectedMinute
        
        let open = UNNotificationAction(identifier: "open", title: "Open ", options: .foreground)
        let cancel = UNNotificationAction(identifier: "cancel", title: "Close", options: .destructive)
        let categories = UNNotificationCategory(identifier: "action", actions: [open,cancel], intentIdentifiers: [])
        UNUserNotificationCenter.current().setNotificationCategories([categories])
        content.categoryIdentifier = "action"
        

            let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
                            
            let randomIdentifier = UUID().uuidString
            let request = UNNotificationRequest(identifier: randomIdentifier, content: content, trigger: trigger)
            
            center.add(request) { error in
                if let error = error {
                    print(error.localizedDescription)
                    
                }
            }
            
        }
}

在我的 DetailView 中(我希望用户在点击通知时登陆)

struct DetailViewNew: View {
    let poemPublisher = NotificationCenter.default.publisher(for: NSNotification.Name("DetailViewNew"))
    @State private var showScreen = false
    
    var body: some View {
        ZStack{
            Text("Text")
        }
        .onReceive(poemPublisher) { notification in
           self.showScreen = true
        }
    }
}

编辑 我删除了类别和操作,因为我只想让用户点击通知或关闭它(不需要自定义操作)。

当我这样做时,if response.actionIdentifier == UNNotificationDefaultActionIdentifier确实会调用完成处理程序。现在的问题是我如何路由它以便在点击时打开 DetailView?

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.actionIdentifier == UNNotificationDismissActionIdentifier {
            print ("Message Closed")
        }
        else if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
            print ("App is Open")
            NotificationCenter.default.post(name: NSNotification.Name("DetailViewNew"), object: nil)
            
        }

    
    completionHandler()
}

有什么建议么?谢谢。

标签: swiftswiftui

解决方案


推荐阅读