当APNS在iOS中收到打开视图控制器 [英] Open view controller when APNS is received in iOS
问题描述
嘿,我是新来的iPhone和我一直在尝试使用苹果推送通知。基本上,我想要做的是,当用户点击接收到推送通知的消息,然后我需要打开一个特定的视图控制器。我已经添加自定义数据与关键参数的类型我的有效载荷JSON,所以代通知类型的价值,我需要,而不是打开主视图控制器的特定视图控制器。
这是我的有效载荷JSON:
{APS:{警告:这是测试消息,类型:通知,徽章:1,声音:默认 }}
我的code是:
- (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions
{
[UIApplication的sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; self.window = [[一个UIWindow页头] initWithFrame:方法[UIScreen mainScreen]界限]]; splashViewController = [[SplashViewController页头] initWithNibName:@SplashViewController捆绑:无];
self.window.rootViewController = splashViewController; [self.window makeKeyAndVisible] 返回YES;
} - (无效)应用:(*的UIApplication)的应用didReceiveRemoteNotification:(NSDictionary的*)USERINFO
{
//这将调用时,你的应用将会从服务器得到任何通知。 [UIApplication的sharedApplication] setApplicationIconBadgeNumber:0];
[UIApplication的sharedApplication] cancelAllLocalNotifications] *的NSDictionary APS =(的NSDictionary *)[USERINFO objectForKey:@APS];
*的NSMutableString = notificationType [APS objectForKey:@型]; 的NSLog(@通知类型是=%@,notificationType); //对于重定向到视图
*的UIViewController的viewController;
如果([notificationType isEqualToString:@通知]){ //更新通知
UpdatesViewController * uvc1 = [[UpdatesViewController页头] initWithNibName:@UpdatesViewController捆绑:无];
的viewController = uvc1;
}
其他{
//投票和放大器; QA
VotingViewController * votingViewController = [[VotingViewController页头] initWithNibName:@VotingViewController捆绑:无];
的viewController = votingViewController;
} [self.window.rootViewController presentViewController:的viewController动画:是完成:NULL];
}
我的code不是presenting其他视图控制器。正如我在code所提到的,我得到了两种Web服务(通知类型)的通知1和2苹果表决。我不知道如何代表通知类型的present特定视图控制器,如果类型是通知,那么我需要开UpdatesViewController否则它将打开其他的ViewController。如果有人知道如何做到这一点,请帮助我。
感谢。
//对于重定向到视图
*的UIViewController UVC;
如果([notificationType isEqualToString:@通知]){ *的UIViewController更新= [[UpdatesViewController页头] initWithNibName:@UpdatesViewController捆绑:无];
//添加更新的具体数据updates.data = [APS objectForKey:@数据];
UVC =更新;
} 否则,如果([notificationType isEqualToString:@投票]){
*的UIViewController投票= [[VotingViewController页头] initWithNibName:@VotingViewController捆绑:无];
//添加投票的具体数据voting.data = [APS objectForKey:@数据];
UVC =投票;
}
[self.window.rootViewController presentViewController:UVC动画:是完成:NULL];
Hey I'm new to iPhone and I have been trying to use an Apple push notification. Basically, what I want to do is that when user clicks on the received push notification message, then i need to open a specific view controller. I have added custom data with key parameter "type" to my payload JSON, so on behalf of notification type value i need to open particular view controller instead of the main view controller.
here is my payload JSON :
{"aps":{"alert":"This is testing message","type":"Notify","badge":1,"sound":"default"}}
my code is :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
splashViewController = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
self.window.rootViewController = splashViewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
//this will call when your app will get any notification from server.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDictionary *aps = (NSDictionary *)[userInfo objectForKey:@"aps"];
NSMutableString *notificationType = [aps objectForKey:@"type"];
NSLog(@"notification type is = %@", notificationType);
//For redirect to the view
UIViewController *viewController;
if([notificationType isEqualToString:@"Notify"]){
//Notify updates
UpdatesViewController *uvc1 = [[UpdatesViewController alloc] initWithNibName:@"UpdatesViewController" bundle:nil];
viewController = uvc1;
}
else {
//Voting & QA
VotingViewController *votingViewController = [[VotingViewController alloc] initWithNibName:@"VotingViewController" bundle:nil];
viewController = votingViewController;
}
[self.window.rootViewController presentViewController:viewController animated:YES completion:NULL];
}
My code is not presenting other view controller. As mentioned in my code, i am getting two kind of web service (notification type) 1. Notify and 2. Voting from Apple. i dont know how to present specific view controller on behalf of notification type, if type is "notify" then i need open UpdatesViewController else it will open other viewcontroller. If anyone knows how to do this, please help me.
Thanks.
//For redirect to the view
UIViewController *uvc;
if([notificationType isEqualToString:@"Notify"]){
UIViewController *updates = [[UpdatesViewController alloc] initWithNibName:@"UpdatesViewController" bundle:nil];
// add updates specific data updates.data = [aps objectForKey:@"data"];
uvc = updates;
}
else if([notificationType isEqualToString:@"Voting "]) {
UIViewController *voting = [[VotingViewController alloc] initWithNibName:@"VotingViewController" bundle:nil];
// add voting specific data voting.data = [aps objectForKey:@"data"];
uvc = voting;
}
[self.window.rootViewController presentViewController:uvc animated:YES completion:NULL];
这篇关于当APNS在iOS中收到打开视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!