如何在推动,而应用程序被关闭打开推送通知新的看法? [英] How to push a new view upon opening push notification while app is closed?
问题描述
所以,当你点击接收推送通知视图时,应用程序打开它有presented观点推动了新的控制器有关通知的详细信息我想要做的是。我使用的是一个UINavigationControllers的UITabBarController。任何帮助将是非常美联社preciated,我试图寻找,但我似乎无法找到任何我指出了正确的方向。低于目前的code:
So what I'm trying to do is when you click view on receiving a push notification when the app opens it has the presented view push a new controller with details regarding the notification. I'm using a UITabBarController with UINavigationControllers. Any help would be much appreciated, I've tried searching but I couldn't seem to find anything that pointed me in the right direction. Current code below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
tabBarController = [[UITabBarController alloc] init];
controller = [[controller alloc] init];
UINavigationController *controller1 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller1.tabBarItem.image = [UIImage imageNamed:@"icon_news.png"];
[controller setTitle:@"View"];
[controller release];
controller = [[controller alloc] init];
UINavigationController *controller2 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller2.tabBarItem.image = [UIImage imageNamed:@"icon_news.png"];
[controller setTitle:@"View"];
[controller release];
controller = [[controller alloc] init];
UINavigationController *controller3 = [[[UINavigationController alloc] initWithRootViewController:controller3] autorelease];
controller3.tabBarItem.image = [UIImage imageNamed:@"icon_news.png"];
[controller setTitle:@"View"];
[controller release];
controller = [[controller alloc] init];
UINavigationController *controller4 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller4.tabBarItem.image = [UIImage imageNamed:@"icon_news.png"];
[controller setTitle:@"View"];
[controller release];
controller = [[controller alloc] init];
UINavigationController *controller5 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller5.tabBarItem.image = [UIImage imageNamed:@"icon_news.png"];
[controller setTitle:@"View"];
[controller release];
tabBarController.viewControllers = [NSArray arrayWithObjects:controller1, controller2, controller3, controller4, controller5, nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
launchDefault = YES;
//[self performSelector:@selector(handlePostLaunch) withObject:nil afterDelay:0];
// Push Notification info
NSDictionary *apns = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSString *result = [[[apns valueForKey:@"aps"] valueForKey:@"alert"] valueForKey:@"loc-args"];
NSString *playerID = [NSString stringWithFormat:@"%@", result];
playerID = [[playerID componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];
playerID = [playerID stringByReplacingOccurrencesOfString:@" " withString:@""];
playerID = [playerID stringByReplacingOccurrencesOfString:@"(" withString:@""];
playerID = [playerID stringByReplacingOccurrencesOfString:@")" withString:@""];
NSLog(@"Player ID: %@", playerID);
if (![playerID isEqualToString:@"null"]) {
if (!detailViewController) {
detailViewController = [[PlayerDetailViewController alloc] init];
}
NSManagedObjectContext *moc = [[AppController sharedAppController] managedObjectContext];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Players"
inManagedObjectContext:moc];
[req setEntity:entity];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"playerID=%@", playerID];
[req setPredicate:pre];
NSError *error;
NSArray *list = [moc executeFetchRequest:req error:&error];
[req release];
Players *player = [list lastObject];
[detailViewController setPlayer:player];
//Want to Push view here
[detailViewController release];
detailViewController = nil;
}
return YES;
}
推荐答案
如果应用被通知启动,然后它会在应用程序委托的应用程序present中:didFinishLaunchingWithOptions:
关键UIApplicationLaunchOptionsRemoteNotificationKey下launchOptions字典,里面有所有的通知有信息(JSON转换为NSDictionary中我相信)。
If application is launched by a notification then it will be present in the application delegate's application:didFinishLaunchingWithOptions:
launchOptions dictionary under the key UIApplicationLaunchOptionsRemoteNotificationKey, which has all the info the notification has (JSON converted to NSDictionary I believe).
编辑:
得到的问题错了,我觉得你要寻找的只是指针当前选定的导航控制器。你得到,如果你查询 [tabbarcontroller selectedViewController]
,它返回可见导航控制器。然后只需按新创建的控制器上的导航控制器的堆栈的顶部。
Got the question wrong, I think what you're looking for is just the pointer to the currently selected navigation controller. You get that if you query [tabbarcontroller selectedViewController]
, which returns the visible navigation controller. Then simply push the newly created controller on top of the stack of that navigation controller.
这篇关于如何在推动,而应用程序被关闭打开推送通知新的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!