嘿,我是iPhone新手,我一直在尝试使用Apple推送通知。基本上,我想要做的是当用户点击收到的推送通知消息时,我需要打开一个特定的视图控制器,它对我有用。我添加了自定义数据和关键参数" type"到我的有效载荷JSON。
这是我的有效载荷JSON:
{"aps":{"alert":"This is testing message","type":"Notify","badge":1,"sound":"default"}}
这是我的代码:
@synthesize viewControllerNotify;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
screenBounds = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if (launchOptions != nil) {
//Launched from push notification
NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
NSMutableString *notificationType = [apsInfo objectForKey:@"type"];
//For redirect to the view
if([notificationType isEqualToString:@"Notify"]){
//Notify updates
UpdatesViewController *uvc1 = [[UpdatesViewController alloc] initWithNibName:@"UpdatesViewController" bundle:nil];
self.viewControllerNotify = uvc1;
}
else if([notificationType isEqualToString:@"Voting"] || [notificationType isEqualToString:@"QA"]){
//Voting & QA
VotingQAViewController *votingQAViewController = [[VotingQAViewController alloc] initWithNibName:@"VotingQAViewController" bundle:nil];
self.viewControllerNotify = votingQAViewController;
}
else if([notificationType isEqualToString:@"Survey"] || [notificationType isEqualToString:@"Quiz"]){
//Survey & Quizzes
SurveysViewController *surveysViewController = [[SurveysViewController alloc] initWithNibName:@"SurveysViewController" bundle:nil];
self.viewControllerNotify = surveysViewController;
}
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewControllerNotify];
self.window.rootViewController = nav;
[nav setNavigationBarHidden:YES];
}
else{
//Normal Launch
if(screenBounds.size.height == 568) {//iPhone5
splashViewController = [[SplashViewController alloc] initWithNibName:@"SplashViewController_5" bundle:nil];
}
else{
splashViewController = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
}
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:splashViewController];
self.window.rootViewController = nav;
[nav setNavigationBarHidden:YES];
}
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSDictionary *aps = (NSDictionary *)[userInfo objectForKey:@"aps"];
int badge = [[aps objectForKey:@"badge"] intValue];
NSMutableString *notificationType = [aps objectForKey:@"type"];
NSLog(@"Number of badge is = %d", badge);
NSLog(@"notification type is = %@", notificationType);
//For redirect to the view
if([notificationType isEqualToString:@"Notify"]){
//Notify updates
UpdatesViewController *uvc1 = [[UpdatesViewController alloc] initWithNibName:@"UpdatesViewController" bundle:nil];
self.viewControllerNotify = uvc1;
}
else if([notificationType isEqualToString:@"Voting"] || [notificationType isEqualToString:@"QA"]){
//Voting & QA
VotingQAViewController *votingQAViewController = [[VotingQAViewController alloc] initWithNibName:@"VotingQAViewController" bundle:nil];
self.viewControllerNotify = votingQAViewController;
}
else if([notificationType isEqualToString:@"Survey"] || [notificationType isEqualToString:@"Quiz"]){
//Survey & Quizzes
SurveysViewController *surveysViewController = [[SurveysViewController alloc] initWithNibName:@"SurveysViewController" bundle:nil];
self.viewControllerNotify = surveysViewController;
}
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewControllerNotify];
self.window.rootViewController = nav;
[nav setNavigationBarHidden:YES];
[self.window makeKeyAndVisible];
}
我的问题是:这是收到推送通知时重定向到视图控制器的正确方法吗?
使用上面的代码,它在forground和后台模式下成功地代表Notification类型(通知有效负载json的自定义键)重定向到视图控制器类,但重定向的View Controller后退按钮不起作用。我不知道我在哪里做错了。如果有人知道那么请帮助我。感谢。
答案 0 :(得分:0)
后退按钮不起作用,因为您将目标视图设置为根视图。您应该构建完整的导航堆栈,即通常,如果您将导航视图控制器作为根视图控制器,则应首先创建该导航堆栈,然后构建根和叶视图之间的所有视图控制器。
例如,如果MainViewController将是您的正常启动视图,并且UpdatesViewController的后退按钮应该导致MainViewController,您可以这样做:
MainViewController *mainVC = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
UpdatesViewController *updatesVC = [[UpdatesViewController alloc] initWithNibName:@"UpdatesViewController" bundle:nil];
UINavigationController *navVC=[[UINavigationController alloc]initWithRootViewController:mainVC];
[navVC setViewControllers:@[mainVC, updatesVC] animated:NO];
self.window.rootViewController = navVC;
答案 1 :(得分:0)
更好您可以使用NSNotificationCentre
中的didReceiveRemoteNotification
发布通知,并接收有关其他课程的通知。这样您就可以从当前视图控制器推送所需的视图控制器(无需设置为根视图控制器)。然后后退按钮就可以了。
对于前。
[[NSNotificationCenter defaultCenter] postNotificationName:kMesssagePushNotification object:nil userInfo:userInfo];
答案 2 :(得分:0)
您有两次机会访问通知数据。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
使用
launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]
访问通知数据并打开您期望的视图控制器。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
如果是后一种情况,你的应用程序将直接调用该函数。你可以使用流动的代码区分它们:
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive){
//notification is received when your app is in background
//open the view controller you expected
}else if(state == UIApplicationStateActive){
//notification is received when your app is in foreground
//do nothing
}
对不起我的英语池,希望有帮助〜