1

I have set up a push notification for my app so that when I click the push notification, the app goes to the main control view. However, I want to view a specific view controller depending on the content that I have added to my app. How can I do this?

My app delegate code.

     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {

          [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
          return YES;
      }

    -(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);

        //  NSLog(@"token ",sendUserToken);

      }

My php push notfication script .

 <?php



    token ="my token"

          $payload = '{
         "aps" :
     {
        "alert" :"'.$message.'",
        "badge" : 1,
        "sound" : "bingbong.aiff"
      }  
   }';
     $ctx = stream_context_create();
     stream_context_set_option($ctx,'ssl', 'local_cert','ck.pem');
     stream_context_set_option($ctx,'ssl','passphrase', 'balpad');
     $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr,60,STREAM_CLIENT_CONNECT,$ctx);
     if(!fp){
       print "Failed to connect $err $errstrn";
       return;
     }else{
        print "notifications sent!";
     }
       $devArray = array();
       $devArray[] = $deviceToken;

      foreach($deviceToken as $token){
      $msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ',' ',$token)).pack("n",strlen($payload)) . $payload;
      print "sending message:" .$payload . "n";
      fwrite($fp,$msg);
     }
     fclose($fp);
     }

  ?>

This the first time I'm using push notifications and I haven't found a proper solution for this. I have found some suggestions (link1 link2) but I find them a little confusing and I'm not getting any ideas. Please somebody guide my how to make this.

Community
  • 1
  • 1
user3349668
  • 147
  • 2
  • 11

3 Answers3

1

i have a solution for you. please refer my example code below:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{       
   UIApplicationState state = [application applicationState];
   if (state == UIApplicationStateActive) {

       // Below Code Shows Message While Your Application is in Active State

       NSString *cancelTitle = @"Ok";

       NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"App Start"
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:cancelTitle
                                              otherButtonTitles: nil];
       [alertView show];
       [alertView release];

    } else {

       // Do stuff that you would do if the application was not active
       // Please add your code to go to specific view controller here.
    }
}
0

Ok do this:

In your .php add another key like:

...
{
        "alert" :"'.$message.'",
        "badge" : 1,
        "sound" : "bingbong.aiff",
        "condition" : "viewController1"
      }
      ...  

you can write there what ever you want. This will tell you what screen you want to show when you get your push, it doesn't need to be a name of your real controller, just some condition so that you can differ your notifications

Then override didReceiveRemoteNotification like this:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{       
   UIApplicationState state = [application applicationState];
   if (state == UIApplicationStateActive) {

       // Below Code Shows Message While Your Application is in Active State

      NSString *strControllerToShow = [[userInfo valueForKey:@"aps"] valueForKey:@"condition"];;
      if(condition != nil){
          if([condition isEqualToString:@"viewController1"]){
              // create vc
              // set properties
              // push it on navigation stack
          }
          if([condition isEqualToString:@"viewController2"]){
              // create vc
              // set properties
              // push it on navigation stack
          }
          ...
      }
    } else {

       // Do stuff that you would do if the application was not active
       // Please add your code to go to specific view controller here.
    }
}

And that is it...

AntonijoDev
  • 1,317
  • 14
  • 29
0

Whenever we are receiving push notification our receive notification method call by ios.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

In this method base on your requirement you can navigate on any view; suppose that you want to navigate on firstviewcontroller, so code is like that:



-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

// here you need to check in which view controller is right now on screen;
//1. if it is same in which you are then no problem just referesh controller;
//otherwise push to your view controller like following"
    firstviewcontroller = [[firstviewcontroller alloc] initWithNibName:@"firstviewcontroller" bundle:nil];

    [self.navigationController pushviewcontroller:firstviewcontroller animated:YES];
}
Rahul Juyal
  • 2,124
  • 1
  • 16
  • 33