2012-07-19 69 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