Как перейти на определенный VC, после нажатия на PUSH-уведомление?

В Swift существует несколько способов перейти на определенный ViewController (VC) после нажатия на PUSH-уведомление. Один из самых распространенных способов - использование метода didReceiveRemoteNotification в AppDelegate.

1. В AppDelegate.swift найдите метод didFinishLaunchingWithOptions и добавьте следующий код:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Уведомление получено, когда приложение было закрыто или находилось в фоновом режиме
    if let notification = launchOptions?[.remoteNotification] as? [String: Any] {
        handleNotification(notification)
    }
    return true
}

2. Добавьте метод didReceiveRemoteNotification в AppDelegate.swift:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    handleNotification(userInfo)
}

3. Создайте функцию handleNotification в AppDelegate.swift для обработки уведомления и перехода на нужный VC:

func handleNotification(_ userInfo: [AnyHashable: Any]) {
    // Извлечение информации из userInfo, если она есть
    if let notificationInfo = userInfo["aps"] as? [String: Any] {
        // Извлечение значения 'targetVC' из userInfo
        if let targetVC = notificationInfo["targetVC"] as? String {
            // Создание экземпляра нужного VC
            let destinationViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: targetVC)
            
            // Навигация на нужный VC
            if let navigationController = window?.rootViewController as? UINavigationController {
                navigationController.pushViewController(destinationViewController, animated: true)
            }
        }
    }
}

4. Отправьте PUSH-уведомление с ключом targetVC в своем серверном коде. Значение ключа targetVC должно быть идентификатором VC, на который вы хотите перейти, например "DetailViewController".

Теперь, когда вы получите PUSH-уведомление приложение откроется и приложение перейдет на нужный VC.