推送通知打开App时执行segue

时间:2015-10-21 14:08:31

标签: ios swift parse-platform notifications swift2

我试图在触摸推送通知时将用户重定向到ViewController。当用户在后台打开应用程序时,此功能正常,但在必须打开应用程序时它无法正常工作。

经过一些研究,我已经了解应用程序何时打开推送通知,它只会读取" didFinishLaunchingWithOptions"。

所以,我现在假设我需要做一些"如果"设置推送通知时发出segue的声明,但我不知道如何检测推送通知,因为这是我第一次在我的应用中使用通知。

这是我的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    Parse.setApplicationId(MY_APP_ID, clientKey: MY_CLIENT_KEY)

    // Registramos la Push Notification
    if application.applicationState != UIApplicationState.Background {

        let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
        let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
        var pushPayload = false
        if let options = launchOptions {
            pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
        }
        if (preBackgroundPush || oldPushHandlerOnly || pushPayload) {
            PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
        }
    }

    if application.respondsToSelector("registerUserNotificationSettings:") {
        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    }
    else {
        //let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound
        //application.registerForRemoteNotificationTypes(types)
        // Access the storyboard and fetch an instance of the view controller
    }

    // Change navigation bar appearance
    UINavigationBar.appearance().barTintColor = UIColor(hex: 0x00B7BB)
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
    if let barFont = UIFont(name: "Avenir Next", size: 20.0) {
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor(), NSFontAttributeName:barFont]
    }
    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

    return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.saveInBackground()
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    if error.code == 3010 {
        print("Push notifications are not supported in the iOS Simulator.")
    } else {
        print("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
    }
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    PFPush.handlePush(userInfo)

    if application.applicationState == UIApplicationState.Inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
    }

    // Access the storyboard and fetch an instance of the view controller
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyboard.instantiateViewControllerWithIdentifier("UltimaMarca")

    // Then push that view controller onto the navigation stack
    let rootViewController = self.window!.rootViewController as! UINavigationController
    rootViewController.pushViewController(viewController, animated: true)
}

你知道一些教程,指南或代码给我看吗?因为我在Objective-C中找到了一些代码行,但它们已经很老了,我无法使用它。

谢谢,

0 个答案:

没有答案