Open view controller when APNS is received in iOS

2019-05-11 03:29发布

问题:

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.

回答1:

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


回答2:

you should check the aps in didFinishLaunchingWithOptions. didReceiveRemoteNotification only works when app is running or suspended.