【问题标题】:On remote push notification redirect to correct ViewController在远程推送通知重定向到正确的 ViewController
【发布时间】:2015-09-26 12:49:29
【问题描述】:

我正在努力让我的 iOS 应用程序在通过远程推送通知(使用 Swift)打开时按预期运行。我想要的是,当通过点击推送通知打开应用程序时,它应该直接跳转到某个 ViewController,但仍保持导航堆栈。更复杂的是,目标视图控制器取决于推送消息。

示例: 我的应用程序已关闭,我收到一条推送通知:“您收到一条新消息”。我单击通知,应用程序打开并显示新消息,而不是常规的初始视图控制器。如果我的应用已打开并且我收到推送通知,则什么也不会发生。

【问题讨论】:

  • 阅读文档后会发现一些一般问题,通过分析代码会发现一些特殊问题。所以我的建议是:阅读相关文档以了解为什么应用程序处于前台时行为会有所不同,检查您的导航代码并确保它执行您希望它执行的操作,如果您遇到问题 - 添加代码 sn -p 并描述哪个部分没有按您的预期工作。

标签: ios swift push-notification


【解决方案1】:

一般来说,以下是响应通知的方法。通过这些方法,您需要实现将根据通知向堆栈显示适当视图的代码。

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

要在您的应用在前台而不是后台运行时响应通知,请实现 application:didReceiveRemoteNotification: 方法

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

【讨论】:

  • 这是一个很好的答案,它回答了我的很多问题 :) 谢谢!
  • @rmp 我的应用程序在从推送中获得重定向时崩溃了,您能帮帮我吗? stackoverflow.com/questions/44335204/…
【解决方案2】:

所以我最终做的是:

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 导航回来。

【讨论】:

    【解决方案3】:

    我在 Objective 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];
    }
    

    编码并享受编码。干杯:)

    【讨论】:

    • 但我也有 tabbarcontroller,在这种情况下,如果使用上面的代码,它会隐藏。
    • @anandprakash 请尝试不使用navigationController
    猜你喜欢
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多