title: How can I open a specific view on notification tap in SwiftUI?
body: <p>I am trying to send a user to a DetailView once the notification is tapped. I've followed multiple instructions such as this <a href="https://stackoverflow.com/questions/60306123/open-a-specific-view-when-opening-the-app-through-a-notification">one</a>. However, when I test it out, tapping on the notification still brings me to the wrong view (the settingsView).</p>
  <p>In App Delegate, I currently have this setup (<strong>for updated code scroll below)</strong>:</p>
  <pre><code>class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
  
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {
          
          registerBackgroundTasks()
          FirebaseApp.configure()
          UNUserNotificationCenter.current().delegate = self
      
          
                  return true
      }
  
  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void) {
  
          if response.actionIdentifier == &quot;open&quot; {
                  NotificationCenter.default.post(name: NSNotification.Name(&quot;DetailViewNew&quot;), object: nil)
              }
          
          completionHandler()
      }
  </code></pre>
  <p>In SettingsDetailView (where the notification is triggered and setup)</p>
  <pre><code>func configureNewAlert() {
      let center = UNUserNotificationCenter.current()
      
      center.removeAllDeliveredNotifications()
      center.removeAllPendingNotificationRequests()
  
          let content = UNMutableNotificationContent()
          content.title = &quot;Header&quot;
          content.body = &quot;Test&quot;
          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: &quot;open&quot;, title: &quot;Open &quot;, options: .foreground)
          let cancel = UNNotificationAction(identifier: &quot;cancel&quot;, title: &quot;Close&quot;, options: .destructive)
          let categories = UNNotificationCategory(identifier: &quot;action&quot;, actions: [open,cancel], intentIdentifiers: [])
          UNUserNotificationCenter.current().setNotificationCategories([categories])
          content.categoryIdentifier = &quot;action&quot;
          
  
              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)
                      
                  }
              }
              
          }
  }
  </code></pre>
  <p>And in my DetailView (where I want the user to land when tapping the notification)</p>
  <pre><code>struct DetailViewNew: View {
      let poemPublisher = NotificationCenter.default.publisher(for: NSNotification.Name(&quot;DetailViewNew&quot;))
      @State private var showScreen = false
      
      var body: some View {
          ZStack{
              Text(&quot;Text&quot;)
          }
          .onReceive(poemPublisher) { notification in
             self.showScreen = true
          }
      }
  }
  </code></pre>
  <p><strong>EDIT</strong>
  I removed the categories and action, as I just want to have the user either tap the notification or dismiss it (not custom actions required).</p>
  <p>When I do so, the completion handler <code>if response.actionIdentifier == UNNotificationDefaultActionIdentifier</code> does indeed get called. Now the question is how do I route it so it opens the DetailView when tapped?</p>
  <pre><code>func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void) {
  
      if response.actionIdentifier == UNNotificationDismissActionIdentifier {
              print (&quot;Message Closed&quot;)
          }
          else if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
              print (&quot;App is Open&quot;)
              NotificationCenter.default.post(name: NSNotification.Name(&quot;DetailViewNew&quot;), object: nil)
              
          }
  
      
      completionHandler()
  }
  </code></pre>
  <p>Any suggestions? Thanks.</p>