2012-12-04 60 views
0

我想執行接收推送通知的任務。我用- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo方法編寫了我的代碼。當應用程序處於前臺時它工作正常,但當應用程序在後臺時,上述方法沒有被調用。有什麼辦法可以在應用程序處於後臺時調用此方法。這個程序是不是提交給iTunes,所以任何好hack詭計? ;)在接收推送通知後執行任務

+0

請更新我,如果這工作或沒有。 –

回答

0

每當應用程序進入後臺時,所有通知都會在backgound線程中發生。所以你需要做這樣的事情。

... 
[[NSNotificationCenter defaultCenter] addObserver:self  
             selector:@selector(processNotificationInBackground:) 
              name:TheNameOfTheNotification 
              object:nil]; 
... 

- (void) processNotificationInBackground:(NSNotification *)not { 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     /* your background notification processing code goes here */ 
     /* note that you should transfer control back to the main queue if you need to update your UI */ 
    } 
} 
+0

它不適用於我:( –

+0

你跟蹤是否生成通知並觀察選擇器被調用嗎? –

+0

是的,這裏選擇器沒有被調用,我測試了它[「[[NSNotificationCenter defaultCenter] postNotificationName:TheNameOfTheNotification對象:自己];「 –