2015-05-30 27 views
0

我希望在用戶點擊視圖控制器時呈現特定的視圖控制器。我知道以前發佈過的同樣的問題有很多問題,並且有很多答案,但似乎沒有一個能夠爲我工作。這裏是我的「的AppDelegate」當通知被點擊時啓動特定的視圖控制器

if(application.applicationState == UIApplicationStateInactive) { 

    NSLog(@"Inactive"); 

    //Show the view with the content of the push 
    if(userInfo) 
    { 
     NSLog(@"In inactive payload"); 
     // Create a pointer to the C object 
     NSString *cId = [userInfo objectForKey:@"c"]; 
     NSLog(cId); 
     PFObject *targetC = [PFObject objectWithoutDataWithClassName:@"C" objectId:cId]; 

     // Fetch C object 
     [targetC fetchInBackgroundWithBlock:^(PFObject *object, NSError *error) { 
      C *c = [[C alloc] initWithPFObject:object]; 

      // Show c view controller 
      if (!error) { 
       NSLog(c.title); 

       CDetailsViewController *viewController = [[CDetailsViewController alloc] initWithC:c]; 
       //[self.navigationController pushViewController:viewController animated:YES]; 
       [self.navigationController presentViewController:viewController animated:YES completion:nil]; 
      } 
     }]; 

    } 

    handler(UIBackgroundFetchResultNewData); 

我得到我在我的推送到確切的數據,我可以從日誌打印我得到見「didReceiveRemoteNotification」方法的代碼。唯一的問題是,我試圖呈現/推送的視圖控制器在通知被點擊時從未看到。任何解決方法?任何我做錯的事情?任何幫助,高度讚賞。

+0

什麼都沒有... – genaks

回答

1

我認爲這是因爲didReceiveRemoteNotification:在後臺線程上調用,可能需要在主線程上調用presentViewController。

以下代碼強制將視圖控制器異步呈現在主線程中Note: a block should never be synchronously run on the main thread。要了解有關Grand Central Dispatch的更多信息,請參閱here

dispatch_async(dispatch_get_main_queue(),^
{ 
    CDetailsViewController *viewController = [[CDetailsViewController alloc] initWithC:c]; 
    [self.navigationController presentViewController:viewController animated:YES completion:nil]; 
} 
+0

你能否詳細說說這裏發生了什麼? – genaks

+0

仍然因爲某種原因不起作用 – genaks

3

如果您的應用程序在後臺,「application:didReceiveRemoteNotification:」方法永遠不會被調用。

您需要添加此,在方法:「應用中:didFinishLaunchingWithOptions:」如果你的應用程序被啓動

// At the end of the method you force call didNotification: 

// Handle any notifications. 
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]!=nil) 
{ 
    NSDictionary * aPush =[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
    [self application:application didReceiveRemoteNotification:aPush]; 

} 


return YES; 
} 

由於用戶點擊一個通知,在launchOptions快譯通,通知有效載荷將被連接。

+0

如果這是需要的,我會添加它。但是我可以看到'didReceiveRemoteNotification'在通知抽頭上被調用,我的數據在'userinfo'中可用 – genaks

相關問題