开发者

How to show a UILocalNotification when ASINetworkQueue finishes all requests?

I'm using ASIHTTPRequest to download multiple files while the iPhone app is running in the background. I want to present a UILocalNotification when the queue finishes. T开发者_如何学运维he following delegate method isn't called until the app is resumed:

- (void)queueFinished:(ASINetworkQueue *)aQueue
{

    NSLog(@"Queue finished");

    if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.alertBody = NSLocalizedString(@"All downloads completed");        
        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
        [localNotification release];
    }
}

So, how can I make this notification appear?


The reason your delegate isn't getting called is likely because your app is suspended in the background. If you are doing some sort of lengthy network process that continues after the user closes the app, you can use -[UIApplication beginBackgroundTaskWithExpirationHandler:] when you start the network tasks so that your application continues running in the background until you're done with the network tasks. However, it can still expire so you're not guaranteed to get enough time to finish.


From previous SO question notification when program is in background iOS 4

You do realize that when your app is in a suspended state, you won't receive any notifications -- and this is right in the documentation. There are only 3 classes of applications that can receive notifications: Audio applications (like iPod and analogues), location based applications, and voip apps. Your plist has to be set up correctly if your app is one of those applications.


Use this:

UILocalNotification *localNot = [[UILocalNotification alloc] init];
localNot.alertBody = @"Your Text";
localNot.alertAction = @"Name on the button";
localNot.fireDate = [NSDate date];
localNot.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNot];

What you're doing:

  • Create the LocalNotification
  • Add the body of your LN
  • Add the name of the button which appears on the "alert"
  • Set the fireDate to the actual date and time (maybe you need to increase the actual date with 1 or 2 seconds - for this use: dateByAddingTimeInterval:)
  • Set the soundName (you could also use a custom sound...)
  • Schedule / Create the LN


Do you have your queue maxConcurrentOperationCount set to 1?

The method setShouldContinueWhenAppEntersBackground:YES is set on a per request basis, and since you have a bunch of ASIHTTPRequest's inside a queue, only one of them may be executing at a time. This means that the other items in your queue haven't even started when you suspend the app, so the OS doesn't know yet to keep that network request alive.

I'm not sure of a solution, but I think this is the reason for what you're seeing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜