2012-07-19 70 views
1

我有一个iphone应用程序,我在做推送通知作为alertview.When我的应用程序处于后台状态时,推送通知即将到来,当我点击它或解锁手机时,它直接进入应用程序,我已经离开它在forground状态。我在警报中添加一个动作,点击查看按钮,它将去往另一个视图控制器。我不想进入应用程序,当我点击通知。我需要显示alertview和视图按钮点击,当我需要做我的action.Can人帮助我实现this.This是我的代码片段` -推送通知警报视图操作?

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    //check application in forground or background 
    if(application.applicationState == UIApplicationStateActive) 
    { 
     //NSLog(@"FOreGround"); 
     //NSLog(@"and Showing %@",userInfo) 
    } 
    else 
    { 
     NSDictionary *curDict= [userInfo objectForKey:@"aps"]; 
     UIAlertView *connectionAlert = [[UIAlertView alloc] initWithTitle:@"app" message:[NSString stringWithFormat:@"%@",[curDict objectForKey:@"alert"]] delegate:self cancelButtonTitle:@"View" otherButtonTitles:@"Cancel",nil]; 
     [connectionAlert show]; 
     [connectionAlert release]; 
     [UIApplication sharedApplication].applicationIconBadgeNumber =[[curDict objectForKey:@"badge"] intValue]; 
    } 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    NSLog(@"applicationWillEnterForeground"); 
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 

    if ([title isEqualToString:@"View"]) 
    { 
     NSArray *mycontrollers = self.tabBarController.viewControllers; 
     NSLog(@"%@",mycontrollers); 
     [[mycontrollers objectAtIndex:0] popToRootViewControllerAnimated:NO]; 
     mycontrollers = nil; 
     tabBarController.selectedIndex = 0; 
    } 
} 
+0

很抱歉,但你能更准确?触摸通知时,您不想进入该应用程序?这根本不可能...... – Yorxxx 2012-07-19 08:49:52

回答

0

您显示UIAlertView中给用户,当通知收到,然后didReceiveRemoteNotification:函数被调用。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    { 
     NSLog(@"userInfo :%@",userInfo); 

     NSString* msg = [userInfo valueForKey:@"aps"]; 

     if (self._VCObj.isViewLoaded && self._VCObj.view.window) { 
       // viewController is visible don't show. 
     } 
      else { // viewController is not visible 
       [[[UIAlertView alloc]initWithTitle:@"Title" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil] show]; 
      } 
     } 
    } 

See Tutorial