在远程推送通知重定向到正确的ViewController

时间:2015-07-08 14:23:44

标签: ios swift push-notification

我在通过远程推送通知(使用Swift)打开时,努力让我的iOS应用程序按预期运行。我想要的是,当通过点击推送通知打开应用程序时,它应该直接跳转到某个ViewController,但仍然维护导航堆栈。并且为了使其进一步复杂化,目标视图控制器依赖于推送消息。

示例:我的应用已关闭,我收到推送通知:"您收到了新消息"。我单击通知,应用程序打开并显示新消息,而不是常规初始视图控制器。如果我的应用程序已打开并且我收到推送通知,则不会发生任何事情。

3 个答案:

答案 0 :(得分:9)

通常,以下是响应通知的方法。通过这些方法,您需要实现代码,该代码将根据通知向您的堆栈显示相应的视图。

要在应用程序在前台或后台运行时响应通知,请执行application:didReceiveRemoteNotification:fetchCompletionHandler:方法。如果您启用了远程通知后台模式,系统将启动您的应用程序(或将其从挂起状态唤醒),并在远程通知到达时将其置于后台状态。但是,如果用户强行退出,系统不会自动启动您的应用程序。

要在应用程序在前台运行时响应通知,请在后台实现application:didReceiveRemoteNotification:方法

要在您的应用未运行时响应通知,请执行application:willFinishLaunchingWithOptions:application:didFinishLaunchingWithOptions:方法

答案 1 :(得分:9)

所以我最终做的是:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        if application.applicationState == .Inactive || application.applicationState == .Background {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let navigationController = self.window?.rootViewController as? UINavigationController
            let destinationController = storyboard.instantiateViewControllerWithIdentifier("dashboard") as? DashboardViewController
            navigationController?.pushViewController(destinationController!, animated: false)
            let destinationController2 = storyboard.instantiateViewControllerWithIdentifier("notificationsSettings") as? AppSettingsTableViewController
            navigationController?.pushViewController(destinationController2!, animated: false)
        }
    }

所以在didReceiveRemoteNotification我检查应用程序来自哪个状态,然后导航到我想要呈现给用户的viewController。我不直接进入ViewController的原因是因为我希望导航堆栈“完整”,以便用户可以通过navigationController导航回来。

答案 2 :(得分:0)

我在目标C 基于 @NikMos 答案

做了同样的事情
// Handle notification messages after display notification is tapped by the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
#if defined(__IPHONE_11_0)
         withCompletionHandler:(void(^)(void))completionHandler {
#else
withCompletionHandler:(void(^)())completionHandler {
#endif


    if (([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) ||
        ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive)) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController *navigationController=[[UINavigationController alloc] init];
        self.window.rootViewController =nil;
        self.window.rootViewController = navigationController;
        ScannerViewController *scannerVc = [storyboard instantiateViewControllerWithIdentifier:@"ScannerID"];
        [navigationController pushViewController:scannerVc animated:YES];
        NotificationVC * notificationVC  = [storyboard instantiateViewControllerWithIdentifier:@"NotificationVCID"];

        [navigationController presentViewController:notificationVC animated:YES completion:nil];

         [self.window makeKeyAndVisible];
}

编码并享受编码。欢呼:)