Search code examples
iosswiftuikitviewcontrollerappdelegate

Change View from Appdelegate depending on push notification


H I need to change the ViewController from my Appdelegate depending on the title shown in my push notification. As you can see in the extension below I am receiving my push notification and testing after I press on it in the foreground mode if it contains the following text: "Dein Freund ist am Joggen". It it contains the text it should switch the viewcontroller. I have tried the methods in the following article:

Opening view controller from app delegate using swift

The main problem is I do not have a window and declaring window: UIwindow? or similar did not help at all. So my question is how can I force to show a Viewcontroller here

I have my storyboard initialized and my requestPage that should be shown.

let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let requestPage = storyboard.instantiateViewController(withIdentifier: "Requester") as! RequestViewController

Here is the complete code of the extension

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

  // Receive displayed notifications for iOS 10 devices.
  func userNotificationCenter(_ center: UNUserNotificationCenter,
                              willPresent notification: UNNotification,
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo

    // Change this to your preferred presentation option
    completionHandler([[.alert, .sound]])
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter,
                              didReceive response: UNNotificationResponse,
                              withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    let application = UIApplication.shared
     
     if(application.applicationState == .active){
        
       print("user tapped the notification bar when the app is in foreground")
        if(userInfo.description.contains("Dein Freund ist am Joggen")) {
            let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let requestPage = storyboard.instantiateViewController(withIdentifier: "Requester") as! RequestViewController
            
        }
     }
     
     if(application.applicationState == .inactive)
     {
       print("user tapped the notification bar when the app is in background")
     }

    completionHandler()
  }
}

Any ideas ?


Solution

  • I fixed it seems to be a common problem so I am happy to share my solution. Only works if you have a Scenedelegate in your project!

    guard var rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else {
               return
           }
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "Requester") as! RequestViewController
    rootViewController.present(controller, animated: true, completion: { () -> Void in
    
    })