2014-03-06 32 views
1

我已經爲我的應用程序設置了推送通知,以便當我單擊推送通知時,應用程序將轉到主控制視圖。但是,我想查看一個特定的視圖控制器,具體取決於我添加到我的應用程序的內容。我怎樣才能做到這一點?IOS 7通過單擊推送通知查看特定視圖控制器

我的應用程序委託代碼。

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

     } 

我的PHP推送通知腳本。

<?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); 
    } 

    ?> 

這第一次我使用推送通知,我還沒有發現這是一個妥善的解決辦法。我發現了一些建議(link1link2),但我覺得他們有點混亂,我沒有得到任何想法。請有人指導我如何做到這一點。

回答

1

我有一個解決方案給你。請參閱我下面的示例代碼:

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

好做到這一點:

在你.PHP添加另一個關鍵,如:

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

,你可以在裏面寫你什麼都想要。這會告訴你什麼屏幕你想要顯示,當你推,它不需要是你的真正的控制器的名稱,只是一些條件,以便你可以不同你的通知

然後覆蓋didReceiveRemoteNotification是這樣的:

-(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. 
    } 
} 

,就是這樣......

+0

小心把太多的信息放到Push中,Apple可以拒絕它:( – Rambatino

0

每當我們收到推送通知通過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]; 
}