UILocalNotification não faz prompts após 10 minutos em segundo plano
EmdidFinishLaunchingWithOptions
um loop temporizador chamando uma funçãohttpRequest
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;
}
Depois de pressionar o aplicativo home do botão está indo ao fundo e chamando a funçãoapplicationDidEnterBackground
então uma tarefa em segundo plano está sendo iniciada.
- (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
função estou recebendoY
a partir do servidor web após cada intervalo de 1 minuto para umUILocalNotification
dispara após 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;
}
FunçãoshowLocalNotification
está chamando após cada 1 minuto com base na resposta dehttpRequest
função.
-(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];
}
Tudo está certo, avisos de notificação após cada 1 munite quando o aplicativo está em segundo plano.
Mas meu problema é que a vida útil da Tarefa em Segundo Plano é de 10 minutos, portanto, após 10 minutos, não há avisos de notificação. Por essa razão, estou iniciando a tarefa Background novamentebeginBackgroundTaskWithExpirationHandler
mas meu aplicativo mata neste momento de reiniciar a tarefa em segundo plano.
Não consegui usar a notificação mais de 10 minutos quando o aplicativo está em segundo plano.
Por favor, alguém me ajude.