推送通知后加载特定的View

问题描述 投票:0回答:3

当我从推送通知打开应用程序时,我需要打开(详细信息视图),我试图找到答案,但我不能。

我附上了显示我需要打开的视图的图像(绿色视图)。请指导我。

enter image description here

ios objective-c
3个回答
0
投票

您可以尝试以下方法:

在AppDelegate类中添加此方法:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
   // Open Your Details view controller directly using segue
   // You can pass details id and then fetch details and display in the view.
    SettingViewController *moreVC = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
    UINavigationController *navigationRootController = [[UINavigationController alloc] initWithRootViewController:moreVC];
    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:navigationRootController animated:YES completion:NULL];
}

这是另一种方法:

  - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

       // Register the below observer in the rootviewcontroller so its registered first and than Pass NSDictionary 
       [[NSNotificationCenter defaultCenter] postNotificationName:@"pushModalPopUp" object:userInfo];
    }

0
投票

Window.rootView> A> B> C.

这是一个简单的方法:

  1. 当您收到通知时,向NSUserDefault添加一个密钥(例如:“NeedShowViewC”= true),然后加载视图控制器A
  2. 在视图控制器的viewDidLoad中,检查该键(“NeedShowViewC”),如果为true,则加载视图B,依此类推。
  3. 完成显示视图控制器C后,将该键设置为false或远程。

0
投票

只是关于Code Hunterr的第一种方法的注释:启动一个新的viewcontroller(你的目标“绿色”VC)+新的UINavigation VC:

SettingViewController *moreVC = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
UINavigationController *navigationRootController = [[UINavigationController alloc] initWithRootViewController:moreVC];

这些将创建这两个VC的新实例。虽然这在功能上可能不是一个问题,但它可能会产生内存管理问题。如果用户在关闭应用程序时已经在查看绿色详细信息视图,则接收推送,结果打开应用程序并再次自动导航到绿色VC,您将拥有2个绿色VC实例内存(你可以检查Xcode调试器内存映射,以便在实践中看到这一点)。如果你反复这样做并且ARC没有释放以前的VC实例(取决于你的MVC实现),你最终可能会膨胀你的内存使用量。

此外,在没有桌面视图的情况下直接显示详细信息VC会使您的“向后”导航陷入困境,因为您刚刚创建的新NavVC在其VC堆栈中没有表视图。

替代方案是:

1)使用didReceiveRemoteNotification()或new(在iOS 10中)获取推送通知UNUserNotificationCenterDelegate

2)使用应用程序的根视图控制器,抓住第一个选项卡的导航控制器 - >指向绿色详细信息视图的导航控制器。

3)然后回滚导航堆栈 - >这将确保无论用户上次打开什么VC /视图,您从头开始导航并取消分配详细信息VC(或您添加的任何未来VC)在分配新的之前,比tableVC更深。

4)然后告诉tableVC,它是你刚抓到的navVC的rootVC,用于显示你需要的行的详细信息。您可以通过向tabelVC添加公共func / API或使用tableVC观察到的通知(如Code Hunterr的第二种方法中所建议的)来完成此操作。

示例(它在swift中但应该很容易转换为ObjC):

//grab the navVC - in this example I'm assuming your tabVC is the rootVC of the app you need the 1st VC of the tabVC's array
if let topNavController = UIApplication.shared.keyWindow?.rootViewController?.viewControllers.first as? UINavigationController {
            //grab the content VC (which is your table VC)
            if let topContentController = topNavController.viewControllers.first as? MyAppsTableView {
                //pop the navVC stack all the way back to its root (the table)
                topNavController.popToRootViewController(animated: false)
                //tell your table view to show detail of a certain row
                topContentController.pleaseShowItem(item: xyzzy)
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.