Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push Notifications - Push a ViewController upon Notification Tap with SceneDelegate

Tags:

ios

swift

Prior to iOS 13, navigation controllers and root views were defined in AppDelegate. With iOS 13 however, Apple introduced SceneDelegate, which takes over the handling of these view functions. However, AppDelegate still handles things such as Local Notification Handling. See this answer for some code that outlines these changes for root views.

If I wanted a view to be pushed when a user taps a local notification, I would do the something like the following in AppDelegate:

extension AppDelegate: UNUserNotificationCenterDelegate {

    var navigationController: UINavigationController?

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        if response.actionIdentifier == UNNotificationDefaultActionIdentifier { //User taps notification
             let vc = MyViewController()
             self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

However, since the root navigation controller for my project can now be defined in SceneDelegate as of iOS 13, I can't seem to figure out how to push a view within a navigation controller managed by SceneDelegate instead of AppDelegate.

like image 315
elrond140 Avatar asked Jan 10 '20 04:01

elrond140


People also ask

What are CleverTap push notifications?

Push notifications provide the capability to communicate brief, yet important alerts to your mobile app users. CleverTap’s rich segmentation and powerful infrastructure lets you send time-sensitive, relevant, and personalized push messages on a large-scale.

What are push notifications and why should you use them?

Not only is it nice to let users know about something that may interest them, be it a new album being released, a sale or other limited-time-only deal, or that one of their friends sent them a message, but push notifications are proven to help boost user interaction and create a better overall user experience.

How can I test push notifications on the simulator?

Additionally, when calling Notifications.requestPermissionsAsync () on the simulator, it will resolve immediately with undetermined as the status, regardless of whether you choose to allow or not. The Expo push notification tool is also useful for testing push notifications during development.

What is appdelegate window in Salesforce?

AppDelegate has a window property that contains all the view controllers that is being displayed. A window is an instance of the UIWindow class 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


1 Answers

SceneDelegate can handle notification response this way:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  func scene(
    _ scene: UIScene,
    willConnectTo session: UISceneSession,
    options connectionOptions: UIScene.ConnectionOptions
  ) {
    if let windowScene = scene as? UIWindowScene {
      let window = UIWindow(windowScene: windowScene)
      // rootViewController set up code
      // say,
      // let mainController = ViewController()
      // let navigationController = UINavigationController(rootViewController: mainController)
      // window.rootViewController = navigationController

      // This is UNNotificationResponse
      if let notificationResponse = connectionOptions.notificationResponse {
        window.makeKeyAndVisible()
        // do the pushing on your navigation controller
        // navigationController.push()
        return
      }

      window.makeKeyAndVisible()
    }
  }
}
like image 73
NeverwinterMoon Avatar answered Sep 21 '22 20:09

NeverwinterMoon