简体   繁体   English

启动特定的viewController以响应远程推送通知

[英]Launching a specific viewController in response to a remote push notification

I'm creating an app who's storyboard flows like the image below: 我正在创建一个应用程序,其故事板如下图所示:

故事板

When a user logs in from "Sysalert View Controller" they are brought into "Message List View Controller" where I do an NSURLConnection to load some JSON into the table. 当用户从“Sysalert View Controller”登录时,它们被带入“消息列表视图控制器”,我在其中执行NSURLConnection以将一些JSON加载到表中。 When a user taps a row in the table, they are brought into "Message Details" which shows more detailed information for that message. 当用户点击表中的一行时,它们将被带入“消息详细信息”,其中显示该消息的更多详细信息。

When a user launches the app from a push notification, regardless of the state of the app prior to launching, I want the app to load the "Message List" data from my server and then show them the message that's just been pushed to the device. 当用户从推送通知启动应用程序时,无论启动之前应用程序的状态如何,我都希望应用程序从我的服务器加载“消息列表”数据,然后向他们显示刚被推送到设备的消息。

I know I need to use didFinishLaunchingWithOptions to tell the app to react to the push notification but how do I setup the view hierarchy so that the "Message List" view controller loads its data and then pushes the "Message Details" view controller onto the stack for the appropriate message? 我知道我需要使用didFinishLaunchingWithOptions来告诉应用程序对推送通知做出反应但是如何设置视图层次结构以便“消息列表”视图控制器加载其数据然后将“消息详细信息”视图控制器推送到堆栈为适当的消息?

Essentially this kind of mimics the behaviour of the Messages or Mail apps. 基本上这种模仿消息或邮件应用程序的行为。 Where opening with a notification brings you to a view controller for that message but you are still able to move back in the hierarchy as if you had launched the app from the initial viewController and traversed through the viewControllers in turn. 如果打开通知会将您带到该消息的视图控制器,但您仍然可以在层次结构中向后移动,就像您从初始viewController启动应用程序并依次遍历viewControllers一样。

It's possible to do what you describe, but I would not recommend it. 你可以做你所描述的,但我不推荐它。

Frist, place a disconnected view controller with the view that you want in the storyboard, give the view controller an identifier, something like "My Push Notification View" Frist,将断开连接的视图控制器与故事板中所需的视图放在一起,为视图控制器提供标识符,例如“我的推送通知视图”

In didFinishLaunchingWithOptions: , You can get to the rootViewController from the app delegate. didFinishLaunchingWithOptions: ,您可以从app delegate获取rootViewController。 This controller will be the navigation controller. 该控制器将是导航控制器。 Using navigation controller, you can push a new view controller on top of the stack. 使用导航控制器,您可以在堆栈顶部推送新的视图控制器。 To create the new view controller, you instantiate the view controller with the identifier "My Push Notification View". 要创建新的视图控制器,请使用标识符“My Push Notification View”实例化视图控制器。

UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
UIViewController *notificationController = [navController.storyboard instantiateViewControllerWithIdentifier:@"My Push Notification View"];

[navController pushViewController:notificationController animated:YES];

I think you will want to use something like -presentViewController:animated:completion: to show a modal view instead of interrupting the navigation stack. 我想你会想要使用类似-presentViewController:animated:completion:来显示模态视图而不是中断导航堆栈。

UIViewController *rootController = (UIViewController *)self.window.rootViewController;
UIViewController *notificationController = [rootController.storyboard instantiateViewControllerWithIdentifier:@"My Push Notification View"];

[rootController presentViewController:notificationController animated:YES completion:NULL];

Try This i used in one of my application, User a variable in app delegate as a global 试试我在我的一个应用程序中使用,用户将app委托中的变量作为全局变量

 ex: BOOL gotNotifcation;

-(void)application:(UIApplication*)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NotificationsViewController *notificationobject = [[NotificationsViewController alloc]init];
    [self.navigationController pushViewController:notificationobject animated:YES];
    gotNotifcation = YES;   
}

In NotificationsViewController for back button action if it is customized button 在NotificationsViewController中,如果是自定义按钮,则返回后退按钮操作

-(void)gotoback
{
    AppDelegate *delegate =(AppDelegate *)[UIApplication sharedApplication].delegate;

    if(delegate.gotNotifcation)
    {
        delegate.gotNotifcation = NO;
        MessageListController *feed = [[MessageListController alloc] init];
        [self.navigationController pushViewController:feed animated:NO];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:NO];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从AppDelegate将远程通知推送到ViewController - Push Remote Notification to ViewController from AppDelegate 在远程推送通知重定向到正确的ViewController - On remote push notification redirect to correct ViewController 在推送通知Swift 2 AppDelegate上加载特定的viewController - Load a specific viewController on push notification Swift 2 AppDelegate 向特定用户发送远程推送通知 - Send remote push notification to specific user 从推送通知 IOS 打开应用程序时打开特定的 ViewController - Open specific ViewController when open app from push notification IOS 当应用程序在 swift 3 中处于前台时隐藏特定视图控制器的远程通知 - Hide remote notification at specific viewcontroller when app is in foreground in swift 3 从远程通知启动时如何在导航堆栈中显示适当的ViewController - How to present appropriate viewcontroller with navigation stack when launching from remote notification 推送特定的viewController - Push a specific viewController 报亭推送通知未启动应用程序 - Newsstand Push Notification not launching Application 解除推送通知所呈现的viewController - Dismissing a viewController presented by a push notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM