Okay, got that fixed… no errors but still not working. I added a ‘print’ in a few places within the notification so I can see when it progresses. Now I’m getting errors in my consul that look like this:
Just trying to figure out something I changed my code to this:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("#1")
if let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window {
print("#1.1")
if let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController {
print("#1.2")
if let tabBarController = rootViewController as? tommyViewController {
print("#1.3")
window.rootViewController = tabBarController
window.makeKeyAndVisible()
print("#2")
}
} else {
print("#3")
let tabBarController = UIStoryboard(name: "main", bundle: .main).instantiateViewController(identifier: "tommyVC") as? tommyViewController
// let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "tommyViewController") as UIViewController
window.rootViewController = tabBarController
window.makeKeyAndVisible()
print("#4")
// self.presentViewController(viewController, animated: false, completion: nil)
}
}
completionHandler()
}
}
When I run it I get this in the consul:
#1.1
#1.2
2021-11-20 20:14:59.336724-0600 KBA[13535:4164500] [connection] nw_read_request_report [C1] Receive failed with error "Software caused connection abort"
2021-11-20 20:15:01.140631-0600 KBA[13535:4164177] [connection] nw_endpoint_handler_set_adaptive_read_handler [C9.6 142.250.190.10:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, ipv6, dns)] unregister notification for read_timeout failed
2021-11-20 20:15:01.140753-0600 KBA[13535:4164177] [connection] nw_endpoint_handler_set_adaptive_write_handler [C9.6 142.250.190.10:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, ipv6, dns)] unregister notification for write_timeout failed
2021-11-20 20:15:01.326647-0600 KBA[13535:4163983] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service
So I’m only printing out the 1.2, tells me there must be something wrong with the iflet tabBarController = rootViewController as ? tommyViewController { code?
In your bundle (the file navigator on the left) is your storyboard named main.storyboard or Main.storyboard?
Remember that in Swift that upper and lower case names are entirely different, meaning that main and Main are not the same.
I’m assuming that what you want to do is when you receive a notification, is to transition to the TabBarController you have named tommyViewController. Is that correct?
If so then try this simple code in place of your userNotificationCenter function when you receive the notification. I’m taking a stab here so comment out your existing function definition before you add this one.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window {
let tabBarVC = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "tommyVC") as? tommyViewController
guard tabBarVC != nil else { return }
// Set it as the root View Controller of the window.
window.rootViewController = tabBarVC
window.makeKeyAndVisible()
}
completionHandler()
}
I’ve got an App here in which I am testing (i’m not using Firebase notifications l) just to see if I can switch from a run of the mill ViewController to a TabBarController on the tap of a button and it works. Just not quite sure if the code is correct when used in AppDelegate on receipt of a notification.
This is the code I finally got to work. I ‘think’ the view controller is assigned the identifier of tommyVC, but the consol keeps giving me an error that it’s not a view controller in the storyboard, but when I run it, it works the way it should.