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 openned 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 ?
Muhammad Noamany is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.