2015-12-23 61 views
0

我有一個應用程序,當應用程序處於活動狀態且我的代碼是這樣的時候,我正在發送解析規範? 在我如何處理ios目標c中的解析通知?

AppDelegate.m 

-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    if([app applicationState] == UIApplicationStateInactive) 
    { 
     //Here I've written 
     [PFPUSH handlePush:UserInfo]; 
    } 
} 

但我需要去perticular頁面上此通知:比如我需要打開觸發推送通知我MUSICViewController。如何從應用程序代理轉到該屏幕; 另一件重要的事情是,如果應用程序未啓動,我如何啓動應用程序並處理通知。 有關信息,一些數據將在應用程序啓動之前從服務器獲取。 請問如何處理它?

+0

一些想法你檢查其他的答案,例如,這一個:http://stackoverflow.com/questions/20757362/open-a -view-controller-when-a-ios-push-notification-is-received –

+0

@Jack你使用storyboard或xib進行佈局嗎? –

+0

@iOSGuru故事板 – Jack

回答

0

如果你使用故事板

UIViewController *controller = [[[[app keyWindow] rootViewController] storyboard] instantiateViewControllerWithIdentifier:@"MUSICViewController"]; 

其他

MUSICViewController *viewController = [[MUSICViewController alloc] init]; 

比目前這種新的VC

[[[app keyWindow] rootViewController] presentViewController:viewController 
                  animated:YES 
                 completion:nil]; 

如果[應用keyWindow] RootViewController的]是導航控制器

UIViewController *viewController = [[UIViewController alloc] init]; 
UINavigationController *nav = [[app keyWindow] rootViewController]; 
[nav pushViewController:nav animated:YES]; 
0

我我分享我的應用程序代碼,您可能會從

- (void)applicationDidReceiveRemoteNotification:(NSDictionary *)userInfo fromState:(UIApplicationState)state 
{ 

if ([UIApplication userId]) { 

    if (state == UIApplicationStateActive) 
    { 
     [[PPAlerts sharedAlerts]showAlertWithType:AlertTypeToast withMessage:[[userInfo valueForKey:@"aps"] valueForKey:@"alert"]]; 
     //[PFPush handlePush:userInfo]; 
    } 


    if (!IS_IPHONE_SIMULATOR) 
    { 
     if ([UIApplication userId]) 
     { 
      PFInstallation *currentInstallation = [PFInstallation currentInstallation]; 
      if (currentInstallation.badge >= 1) 
      { 
       long k = currentInstallation.badge-1; 
       [[UIApplication sharedApplication] setApplicationIconBadgeNumber:k]; 
       currentInstallation.badge = k; 
       [currentInstallation saveInBackground]; 
      } 
     } 
    } 

    if ([[userInfo objectForKey:@"t"] isEqualToString:@"request"] || [[userInfo objectForKey:@"t"] isEqualToString:@"t"]) { 
     self.isRequest = true; 
    } 
    else 
    { 
    self.isRequest = false; 
    } 

    NSLog(@"1-->user"); 
    if ([UIApplication userId] && [[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) 
    { 
     NSLog(@"2-->inactive"); 
     if (self.sidePanelViewController==nil) { 
      NSLog(@"3-->forcestop"); 
      LoginViewController *login= [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil]; 
      self.isNotifiction=true; 

      self.notificationID = [userInfo numberForJson:@"id"]; 

      [self openMenuViewcontroller:login]; 
     } 
     else 
     { 
      NSLog(@"4-->runnigBG"); 
      if (self.isRequest) { 
       self.tabBarController.selectedIndex = 4; 

      } 
      else 
      { 
      NotificationViewController *notif=[[NotificationViewController alloc]initWithNibName:@"NotificationViewController" bundle:nil]; 
      notif.isParseNotifiction=true; 
      self.notificationID = [userInfo numberForJson:@"id"]; 
      SuperNavigationController *navNoti = [[SuperNavigationController alloc]initWithRootViewController:notif]; 
      self.sidePanelViewController.centerPanel = navNoti; 
      } 
     } 
    } 
    else 
    { 
     // [[APIRequest request]HomeCount:@"0" completed:nil]; 
     if (appDelegate.isLogin) { 
     [[APIRequest request]NotifictionCount:@"0" completed:nil]; 
     [[APIRequest request]invitationCount:@"0" completed:nil]; 
     [[APIRequest request]whislistCount:@"0" completed:nil]; 
     } 
     NSLog(@"5-->active"); 
     if(self.isRequest) 
     { 
      self.isRequest = false; 
      if (self.isRequestOpen) { 
       [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 
       [[PFInstallation currentInstallation] setBadge:0]; 
       [[PFInstallation currentInstallation] saveEventually]; 
      } 

     } 
     else if (appDelegate.isNotifiction) 
     { 
      [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 
      [[PFInstallation currentInstallation] setBadge:0]; 
      [[PFInstallation currentInstallation] saveEventually]; 
      [[APIRequest request]NotifictionAll:appDelegate.UserID ShowLoader:NO completed:nil]; 
     } 
     else 
     { 

     } 
    } 

} 

} 
+0

嘿Thnaks,但不能理解你的代碼。在didrecieveRemoteNotiicationMethod中的應用委託方法中,如果應用在後臺。我應該怎麼做,請寫那麼多... – Jack

+0

你的應用程序在後臺,然後'if([[UIApplication sharedApplication] applicationState]!= UIApplicationStateActive)'編寫你的代碼以在這種情況下打開MUSICViewController – jay