How Do I Read a Notification Thats Embeded*
Note: If your app project has SceneDelegate.swift (created in Xcode 11, and only target iOS 13 and higher up) , delight read this mail which cater for push button notification tap with scenedelegate
Say you accept an app and want to redirect user to a specific view when a push notification is tapped, eg: going to a specific chat room in Telegram later borer push notification of that bulletin. How practise we proceed to implement this? 🤔
In a blitz and just desire the code solution? Click hither
(Notice that subsequently tapping the button notification, the app moves to the conversation with Sans).
In the previous article on performing action when user tap on notification, we know that to perform an activeness when user tap on notification, we need to use the didReceive response: method from UNUserNotificationCenterDelegate.
// AppDelegate.swift // remember to ready consul for notification center // UNUserNotificationCenter.current().delegate = cocky extension AppDelegate: UNUserNotificationCenterDelegate{ // This function will be called correct afterwards user tap on the notification func userNotificationCenter(_ heart: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { // tell the app that we have finished processing the user's action / response completionHandler() } } Now that we can procedure the notification tap, how practice we admission the view controllers from AppDelegate?
Window and RootViewController
AppDelegate has a window property that contains all the view controllers that is being displayed.
From Apple's Documentation,
A window is an example of the
UIWindowclass and handles the overall presentation of your application's user interface. Windows work with views (and their owning view controllers) to manage interactions with, and changes to, the visible view hierarchy
Every iOS awarding needs at least one window—an instance of the
UIWindowclass—and some may include more than i window. A window object has several responsibilities:
- It contains your application's visible content.
- It plays a fundamental role in the delivery of impact events to your views and other awarding objects.
- Information technology works with your awarding'southward view controllers to facilitate orientation changes.
Yous can think of UIWindow as a container that contains your app views (and their owning view controllers), view controllers are usually stored in a stack (think of it like layers in Photoshop).
If we click on the debug hierachy button in Xcode while the app is running, we tin come across the 3D arrangement of the view controllers and views. Find that UIWindow is on the furthest back in the hierachy, the tab bar controller is stacked on top of information technology, so the navigation controller, and finally the specific view controller.
We tin can access the window property (UIWindow object) in AppDelegate by using self.window? , cocky refers to the AppDelegate, and the window property is optional, it might exist nil in instance there is no window, but very unlikely.
There's a rootViewController property for the window object, which access the most bottom view controllers in the stack. In the illustration in a higher place, the most bottom view controller is the UITabBarController, this is the rootViewController for the window.
In storyboard, the flow looks like this:
The root view controller is our entry point to access view controllers from AppDelegate. One naive approach would exist changing the root view controller to the view controller nosotros want when the user tap on the button notification.
Say we desire to show the ConversationViewController when user tap on push notification :
Nosotros tin can change the root view controller like this :
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let storyboard = UIStoryboard(name: "Main", parcel: nil) // instantiate the view controller from storyboard if let conversationVC = storyboard.instantiateViewController(withIdentifier: "ConversationViewController") as? ConversationViewController { // set the view controller as root self.window?.rootViewController = conversationVC } // tell the app that we have finished processing the user'southward action / response completionHandler() }
The trouble with this arroyo is that past replacing the root view controller with the new view controller, we have removed the whole current stack including tab bar controller and also the navigation controller, making the user unable to go back to the previous view controller! 😱
Pushing new view controller into the current navigation controller
A better arroyo would be pushing the new view controller to the existing navigation controller.
This section presume that your app uses a navigation controller to motility around view controllers. All tabs in the tab bar controller incorporate a navigation controller.
As the tab bar controller is the root view controller, we can traverse to the navigation controller, and push button the new view controller into it like this :
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let storyboard = UIStoryboard(name: "Main", bundle: goose egg) // instantiate the view controller we want to show from storyboard // root view controller is tab bar controller // the selected tab is a navigation controller // and then we push the new view controller to it if let conversationVC = storyboard.instantiateViewController(withIdentifier: "ConversationViewController") as? ConversationViewController, let tabBarController = self.window?.rootViewController as? UITabBarController, let navController = tabBarController.selectedViewController as? UINavigationController { // nosotros can modify variable of the new view controller using notification information // (eg: title of notification) conversationVC.senderDisplayName = response.notification.request.content.title // you can access custom data of the push notification by using userInfo property // response.notification.asking.content.userInfo navController.pushViewController(conversationVC, animated: true) } // tell the app that we have finished processing the user's activeness / response completionHandler() } This code will produce the following outcome :
Want to go the Xcode project file containing the consequence above and endeavor it out yourself? 👇
https://github.com/fluffyes/tapPushOpenView/archive/principal.zip
Presenting view controller modally
What if the current showing view controller of your app isn't independent inside a navigation controller? Yous tin can choose to present it modally (call up to check if there is any other view controller is being presented before presenting information technology), but you would have to traverse from the root view controller to the current showing view controller manually in AppDelegate, and then phone call .present() on the electric current view controller.
if let conversationVC = storyboard.instantiateViewController(withIdentifier: "ConversationViewController") as? ConversationViewController, permit tabBarVC = self.window?.rootViewController every bit? UITabBarController { tabBarVC.selectedViewController.nowadays(conversationVC, animated: true, completion: nil) } Tired of fighting with Auto Layout constraints? Why is information technology and then difficult to make a layout to piece of work?! Would using code for UI and constraints go far easier? (No, not really) If you want to understand Machine Layout fundamentally (instead of only post-obit youtube tutorials implementing a very specific layout which might non utilise to your app), bank check out my volume Making Sense of Automobile Layout, with applied instance study!
"It helped me sympathize how Auto Layout works fundamentally - something that I couldn't find in any tutorial. I realized what Machine Layout needs for each view to render it correctly" – Ostik Lebyak
Source: https://fluffy.es/open-specific-view-push-notification-tapped/
Post a Comment for "How Do I Read a Notification Thats Embeded*"