在 iOS 中接收到 APNS 时打开视图控制器 [英] Open view controller when APNS is received in iOS

查看:20
本文介绍了在 iOS 中接收到 APNS 时打开视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我是 iPhone 的新手,我一直在尝试使用 Apple 推送通知.基本上,我想做的是当用户点击收到的推送通知消息时,我需要打开一个特定的视图控制器.我已将带有关键参数type"的自定义数据添加到我的有效负载 JSON,因此代表通知类型值,我需要打开特定的视图控制器而不是主视图控制器.

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.

这是我的有效载荷 JSON :

here is my payload JSON :

{"aps":{"alert":"This is testing message","type":"Notify","badge":1,"sound":"default"}}

我的代码是:

- (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];
}

我的代码没有呈现其他视图控制器.正如我的代码中提到的,我得到了两种 Web 服务(通知类型)1. 通知和 2. 来自 Apple 的投票.我不知道如何代表通知类型呈现特定的视图控制器,如果类型是通知",那么我需要打开 UpdatesViewController 否则它将打开其他视图控制器.如果有人知道如何做到这一点,请帮助我.

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.

谢谢.

推荐答案

//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];

这篇关于在 iOS 中接收到 APNS 时打开视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆