How to open specific view controller when push not

2019-02-11 06:21发布

I got stuck specific view controller is not move when I tap on push notification alert when application is not open stage totally.

Here is my code:

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()
        }
    }
}

I can get notification and tap to move specific VC when application is active. Please help me what I am missing.

标签: ios swift3
2条回答
地球回转人心会变
2楼-- · 2019-02-11 07:21

When you app is in closed state you should check for launch option in " func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool{ " and call your API.

Example:

if let option = launchOptions {
        let info = option[UIApplicationLaunchOptionsKey.remoteNotification]
        if (info != nil) {
             goAnotherVC()
    }}
查看更多
够拽才男人
3楼-- · 2019-02-11 07:27

Swift 4

I have developed this and it works perfectly !

Either you are in foreground, background or the app is totally closed.

In 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()
}

Good luck !

查看更多
登录 后发表回答