user3427551
user3427551

Reputation: 345

Navigate to particular view controller when user tap the notification IOS7

Hi i have added push notification in my application and I want to view particular ViewControllers when the user tap the notification. In my app delegate m file I'm trying to get the register the device token to my server and from my server I'm using the php script to get the device token from server and I'm sending the notification.

The problem here I'm trying to view a particular view controller when the user taps on the notification its not working i have tried many different methods nothing had worked.

Here I'm view the popup like to send notification from the app when user trying to install application for the first time.

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
           [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
            return YES;
 }


- (void) clearNotifications {
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
 }

Here I'm storing the device to token to my server.

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
 {

    const char* data = [deviceToken bytes];
    NSMutableString * token = [NSMutableString string];

   for (int i = 0; i < [deviceToken length]; i++) {
      [token appendFormat:@"%02.2hhX", data[i]];
 }


     NSString *urlString = [NSString stringWithFormat:@"url?token=%@",token];

     NSURL *url = [[NSURL alloc] initWithString:urlString];
     NSLog(@"token %@",urlString);


     NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
     NSLog(@"request %@ ",urlRequest);
     NSData *urlData;
     NSURLResponse *response;
     urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];    
     NSLog(@"data %@",urlData);
     [self clearNotifications];
     //  NSLog(@"token ",sendUserToken);

    }

Here I'm trying to view the particular method when user tap the notification.

   -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
           updatepoliticalViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"updatepoliticalViewController"];
           [self.window.rootViewController presentViewController:ringingVC animated:YES completion:NULL];
       }

My particular view controller name is updatepoliticalViewController its a navigation view controller please tell me in this above code where I'm doing wrong how to resolve this issue.

Thanks

Upvotes: 2

Views: 3328

Answers (2)

Rahul Patel
Rahul Patel

Reputation: 5886

1) When application is running in background and When application is running in foreground application:didReceiveRemoteNotification: method will called as below.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive)
     {
     //opened from a push notification when the app was on background

       NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
     }
  else if(application.applicationState == UIApplicationStateActive)
     {
     // a push notification when the app is running. So that you can display an alert and push in any view

        NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
     }
}

2) When application is not launched (close) than application:didFinishedLaunchWithOptionsmethod will called.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  if (launchOptions != nil)
    {
         //opened from a push notification when the app is closed
        NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo != nil)
        {
             NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
        }

    }
     else{
             //opened app without a push notification.
         }
}

Upvotes: 0

Greg
Greg

Reputation: 25459

When the app is in foreground state it call

application:didReceiveRemoteNotification:

but if it's not and the app is launched, for example, by swiping the alert in notification center

application:didFinishLaunchingWithOptions:

is called with key. The good way is to call

application:didReceiveRemoteNotification:

from

application:didFinishLaunchingWithOptions:

I believe this should help

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
        [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
    }
    // YOUR CODE...
    return YES;
}

// EXTENDED

Try get storyboard like that:

// Make sure the name match
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

Create your object from mainstoryboard:

updatepoliticalViewController *ringingVC = [mainstoryboard instantiateViewControllerWithIdentifier:@"updatepoliticalViewController"];

And try set new root view controller

[self.window setRootViewController: ringingVC];

Upvotes: 3

Related Questions