UILocalNotification在10分钟后不会提示

didFinishLaunchingWithOptions一个定时器循环每隔1分钟调用一次函数httpRequest

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //rest of code NSTimer *notifyTimer = [NSTimer timerWithTimeInterval:60 target:self selector:@selector(httpRequest) userInfo:nil repeats:YES];//7200.0 [[NSRunLoop mainRunLoop] addTimer:notifyTimer forMode:NSDefaultRunLoopMode]; return YES; } 

在按下homebutton后,应用程序将会后台调用函数applicationDidEnterBackground所以后台任务正在启动。

 - (void)applicationDidEnterBackground:(UIApplication *)application { __block UIBackgroundTaskIdentifier bgTask; UIApplication *app = [UIApplication sharedApplication]; expirationHandler = ^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler]; }; bgTask = UIBackgroundTaskInvalid; bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler]; } 

通过httpRequest函数,我每隔1分钟就从Web服务器创build一个Y ,所以一个UILocalNotification会在UILocalNotification之后触发。

 -(NSString *)httpRequest { NSURL *url = [NSURL URLWithString:@"http://192.168.10.67/t.php"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; NSString *userAgent = [NSString stringWithFormat:@"bgTaskTest-IOS"]; [request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; [request setHTTPMethod:@"GET"]; [request setTimeoutInterval:25]; NSURLResponse *response; NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; NSString *stringReply = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding]; if ([stringReply isEqualToString:@"Y"]) { [self showLocalNotification:nil]; //calling UILocalNotification } else { NSLog(@"%@",stringReply); } return stringReply; } 

根据httpRequest函数的响应,每1分钟调用一次httpRequest函数。

 -(void)showLocalNotification { NSString *msg = @"test message"; [[UIApplication sharedApplication] cancelAllLocalNotifications]; UILocalNotification *_localNotification = [[UILocalNotification alloc]init]; _localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1]; _localNotification.timeZone = [NSTimeZone defaultTimeZone]; _localNotification.alertBody = msg; _localNotification.soundName = UILocalNotificationDefaultSoundName; _localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1; [[UIApplication sharedApplication] scheduleLocalNotification:_localNotification]; //[[UIApplication sharedApplication] presentLocalNotificationNow:_localNotification]; } 

一切正常,申请后台每隔1分钟后通知提示。

但是我的问题是后台任务的生命时间是10分钟,所以10分钟后没有通知提示。 由于这个原因,我在beginBackgroundTaskWithExpirationHandler再次启动后台任务,但是我的应用程序在此时重新启动后台任务。

当应用程序处于后台时,我无法使用超过10分钟的通知。

请有人帮助我。

在app store的指导下,没有办法在后台运行超过10分钟的任意代码(正如您已经注意到的那样)。

10分钟后,您的应用将被暂停。 有几种方法可以注册其他背景模式(如背景audio,连续播放无声音文件)或后台voip或后台位置服务。

这些hacky工作将保持您的应用程序不挂起,但是您的应用程序将不会被批准的商店。

在iOS7中有进步在后台运行代码,但没有什么会做你想要的。

所以,如果这是一个你自己使用的应用程序,使用私人API或我上面build议的方法,但是如果你想在商店得到这个应用程序,恐怕你运气不好。