UILocalNotification no recibe indicaciones después de 10 minutos en segundo plano
EndidFinishLaunchingWithOptions
un bucle temporizador llamando a una funciónhttpRequest
cada intervalo de 1 minuto.
- (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;
}
Después de presionar el botón de inicio, la aplicación pasa al fondo y llamaapplicationDidEnterBackground
así que una tarea de fondo está comenzando.
- (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];
}
PorhttpRequest
función estoy consiguiendoY
desde el servidor web después de cada intervalo de 1 minuto por lo que unaUILocalNotification
Dispara después de cada segundo.
-(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;
}
FunciónshowLocalNotification
está llamando después de cada 1 minuto basado en la respuesta dehttpRequest
función.
-(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];
}
Todo está bien, la notificación aparece después de cada munite cuando la aplicación está en segundo plano.
Pero mi problema es que la vida útil de Background Task es de 10 minutos, por lo que después de 10 minutos no hay avisos de notificación. Por esta razón estoy comenzando de nuevo la tarea de fondo enbeginBackgroundTaskWithExpirationHandler
pero mi aplicación mata en este momento de reiniciar la tarea en segundo plano.
No pude usar la notificación más de 10 minutos cuando la aplicación está en segundo plano.
Por favor, que alguien me ayude.