1

I have a multiple root navigationView in swiftui view, I am setting the root based on a state variable, the issue is that when I am navigating to a screen from a push notification click and then click back, it returns me to the root view not the one was opened before the click of the push notification.

For example: I am navigating A -> B -> C, then open D screen from the push notification, click back, it returns me to A not to C

I am navigating to the push details screen based on a state variable in the content view screen.

Main app code:

@main
struct Push_Notification_POCApp: App {
    @UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
    @StateObject var notificationManager = NotificationManager()
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onAppear{
                    appDelegate.app = self
                    Task{
                        await notificationManager.request()
                    }
                }
        }
    }
}

ContentView code:

struct ContentView: View {
    
    @State var navigateTpPushNotificationPage = false
    
    @State var rootPage = 0
    
    var body: some View {
        NavigationView {
            VStack {
                switch rootPage {
                case 0:
                    ScreenA()
                case 1:
                    ScreenB()
                case 2:
                    ScreenC()
                default:
                    ScreenA()
                }
                
                NavigationLink(destination: ScreenFromPuah(), isActive: $navigateTpPushNotificationPage, label: {
                    EmptyView()
                })
                .hidden()
            }
            .padding()
            .onAppear {
                NotificationCenter.default.addObserver(forName: Notification.Name("NavigateToScreen"), object: nil, queue: .main) { notification in
                    navigateTpPushNotificationPage = true
                    
                }
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct ScreenA: View {
    var body: some View {
        NavigationLink(destination: ScreenB()) {
            Text("Open Screen B")
        }
    }
}

struct ScreenB: View {
    var body: some View {
        NavigationLink(destination: ScreenC()) {
            Text("Open Screen C")
        }
    }
}

struct ScreenC: View {
    var body: some View {
        Text("Open Screen C")
    }
}

struct ScreenFromPuah: View {
    var body: some View {
        Text("Opened From Push")
    }
}

AppDelegate code:

import Foundation
import UIKit

//MARK: AppDelegate
class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
    var app: Push_Notification_POCApp?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        application.registerForRemoteNotifications()
        UNUserNotificationCenter.current().delegate = self
        
        return true
    }
    
    func application(_ application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let stringifiedToken = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("stringifiedToken:", stringifiedToken)
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        NotificationCenter.default.post(name: Notification.Name("NavigateToScreen"), object: "open page c")
        completionHandler()
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
        return [.badge, .banner, .list, .sound]
    }
}

So how to navigate from push notification without losing the current navigation stack ?

2
  • Note, NavigationView is deprecated you should use NavigationStack. Try using NavigationStack with NavigationStack(path: $path) and navigationDestination(...). Commented Jul 29, 2024 at 22:33
  • The project has a minimum deploy target of ios 14, NavigationStack can't be used in ios 14. @workingdogsupportUkraine Commented Jul 30, 2024 at 13:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.