如何在swift 3中收到推送通知时打开特定的视图控制器

时间:2017-03-22 00:27:32

标签: ios swift3

当应用程序完全没有打开阶段时,当我点击推送通知警报时,我被卡住特定视图控制器不动。

这是我的代码:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    /*

    fetch and add push notification data

     */
    goAnotherVC()
}

func goAnotherVC() {
    if (application.applicationState == UIApplicationState.active) {
        /* active stage is working */ 
    } else if (application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background) {
        if (type == "1" || type == "2") {
            let storyboard: UIStoryboard = UIStoryboard(name: "MyAppointments", bundle: nil)
            let apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationDetailViewController") as! NotificationDetailViewController
            let navigationController = UINavigationController.init(rootViewController: apptVC)
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()
        } else if (type == "3") {
            let storyboard: UIStoryboard = UIStoryboard(name: "MyAppointments", bundle: nil)
            let apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationDetailViewController") as! NotificationDetailViewController
            let navigationController = UINavigationController.init(rootViewController: apptVC)
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()
        } else if (type == "4") {
            let storyboard: UIStoryboard = UIStoryboard(name: "Enquiry", bundle: nil)
            let enqVC = storyboard.instantiateViewController(withIdentifier: "EnquiryDetailViewController") as! EnquiryDetailViewController
            let navigationController = UINavigationController.init(rootViewController: enqVC)
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()
        }
    }
}

我可以获得通知,并在应用程序处于活动状态时点按以移动特定VC。请帮助我,我错过了什么。

3 个答案:

答案 0 :(得分:6)

当你的应用处于关闭状态时,你应该检查启动选项 " func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { "并致电您的API。

示例:

if let option = launchOptions {
        let info = option[UIApplicationLaunchOptionsKey.remoteNotification]
        if (info != nil) {
             goAnotherVC()
    }}

答案 1 :(得分:0)

Swift 5,iOS 13 -

由于 iOS 13“窗口”在 SceneDelegate 中可用。但是 didReceiveNotification 方法仍然存在于 AppDelegate 中。

所以你必须首先从SceneDelegate

访问窗口
let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window

现在您可以设置窗口的rootViewController属性

    window.rootViewController = viewControllerObject
    window.makeKeyAndVisible()

答案 2 :(得分:-2)

雨燕4

我已经开发了它,并且效果很好!

您处于前台,背景中,或者应用已完全关闭。

在AppDelegate中:

//
// FCM - Firebase Cloud Messaging
//

// The callback to handle data message received via FCM - BACKGROUND / ClOSED
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage)
{
    print(remoteMessage.appData)
}

// This method will be called when app received push notifications - FOREGROUND
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    completionHandler([.alert, .badge, .sound])
}

// This method is called when user CLICKED on the notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
    print("user clicked on the notification")
    let userInfo = response.notification.request.content.userInfo
    let targetValue = userInfo["target"] as? String ?? "0"

    if targetValue == "0"
    {
        Helper.instance.goToControlllerWith(ID: .UserOrdersNavController)
    }
    else if targetValue == "1"
    {
        Helper.instance.goToControlllerWith(ID: .ComingOrdersNavController)
    }

    completionHandler()
}
  

注意:Helper.instance.goToControlllerWith(ID: .UserOrdersNavController)是iOSKit中不存在的辅助方法。您可以使用instantiateViewController

创建自己的

祝你好运!