[英]Launching a specific viewController in response to a remote push notification
我正在创建一个应用程序,其故事板如下图所示:
当用户从“Sysalert View Controller”登录时,它们被带入“消息列表视图控制器”,我在其中执行NSURLConnection以将一些JSON加载到表中。 当用户点击表中的一行时,它们将被带入“消息详细信息”,其中显示该消息的更多详细信息。
当用户从推送通知启动应用程序时,无论启动之前应用程序的状态如何,我都希望应用程序从我的服务器加载“消息列表”数据,然后向他们显示刚被推送到设备的消息。
我知道我需要使用didFinishLaunchingWithOptions
来告诉应用程序对推送通知做出反应但是如何设置视图层次结构以便“消息列表”视图控制器加载其数据然后将“消息详细信息”视图控制器推送到堆栈为适当的消息?
基本上这种模仿消息或邮件应用程序的行为。 如果打开通知会将您带到该消息的视图控制器,但您仍然可以在层次结构中向后移动,就像您从初始viewController启动应用程序并依次遍历viewControllers一样。
你可以做你所描述的,但我不推荐它。
Frist,将断开连接的视图控制器与故事板中所需的视图放在一起,为视图控制器提供标识符,例如“我的推送通知视图”
在didFinishLaunchingWithOptions:
,您可以从app delegate获取rootViewController。 该控制器将是导航控制器。 使用导航控制器,您可以在堆栈顶部推送新的视图控制器。 要创建新的视图控制器,请使用标识符“My Push Notification View”实例化视图控制器。
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
UIViewController *notificationController = [navController.storyboard instantiateViewControllerWithIdentifier:@"My Push Notification View"];
[navController pushViewController:notificationController animated:YES];
我想你会想要使用类似-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];
试试我在我的一个应用程序中使用,用户将app委托中的变量作为全局变量
ex: BOOL gotNotifcation;
-(void)application:(UIApplication*)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NotificationsViewController *notificationobject = [[NotificationsViewController alloc]init];
[self.navigationController pushViewController:notificationobject animated:YES];
gotNotifcation = YES;
}
在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.