在后台接收推送通知时执行任务

时间:2012-12-04 06:07:48

标签: ios objective-c iphone push-notification

我想在接收推送通知时执行任务。我用- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo方法编写了代码。应用程序在前台时工作正常,但是当应用程序在后台时,上述方法不会被调用。有什么方法可以在应用程序处于后台时调用此方法。这个应用程序不适用于提交给iTunes,所以任何好的黑客技巧? ;)

1 个答案:

答案 0 :(得分:0)

每当应用程序进入后台时,所有通知都会在后台线程中发生。你需要做这样的事吗。

...
[[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 */
    }
}