后台任务即将到期时的本地通知

Rit*_*ika 1 background ios uilocalnotification

iOS应用程序可以在后台任务即将到期时安排本地通知吗?当应用程序使用NSOperationQueue进入后台时,基本上我有一些服务器端正在进行下载.
我想要的是当后台任务即将完成时通过本地通知通知用户.那么该用户可以将应用程序带到前台以保持持续的服务器数据下载
以下是我正在使用的代码,但我没有看到任何本地通知

UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
           /*TO DO
            prompt the user if they want to continue syncing through push notifications. This will get the user to essentially wake the app so that sync can continue.
             */
            // create the notification and then set it's parameters
            UILocalNotification *beginNotification = [[[UILocalNotification alloc] init] autorelease];
            if (beginNotification) {
                beginNotification.fireDate = [NSDate date];
                beginNotification.timeZone = [NSTimeZone defaultTimeZone];
                beginNotification.repeatInterval = 0;
                beginNotification.alertBody = @"App is about to exit .Please bring app to background to continue dowloading";
                beginNotification.soundName = UILocalNotificationDefaultSoundName;
                // this will schedule the notification to fire at the fire date
                //[app scheduleLocalNotification:notification];
                // this will fire the notification right away, it will still also fire at the date we set
                [application scheduleLocalNotification:beginNotification];
            }

            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];
Run Code Online (Sandbox Code Playgroud)

Tri*_*ops 5

我相信您的代码的问题是dispatch_async调用.这是来自docs的东西:

-beginBackgroundTaskWithExpirationHandler:
(...)在主线程上同步调用处理程序,从而在通知应用程序时暂时阻止应用程序的暂停.

这意味着在此到期处理程序完成后,您的应用程序将立即暂停.您正在主队列上提交异步块,因为这实际上是主队列(请参阅文档),它将在稍后执行.

对此的解决方案不是调用dispatch_async,而是直接在此处理程序中运行该代码.

我看到的另一个问题是,在过期处理程序中通知用户为时已晚,应该在到期之前完成(比如一分钟左右).您只需要定期检查backgroundTimeRemaining并在达到您的间隔时显示此警报.