didFinishLaunchingWithOptions
에서 타이머 루프는 1 분 간격으로 httpRequest
함수를 호출합니다.백그라운드에서 10 분 후에 UILocalNotification 프롬프트가 표시되지 않습니다.
- (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;
}
기능 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
기능에 의해 나는 그래서 UILocalNotification
화재 매 초 후 매 1 분 간격 후 웹 서버에서 Y
을에 geting하고있다.
-(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;
}
기능 showLocalNotification
는 httpRequest
함수의 응답에 기초하여 1 분마다 후에 호출된다.
-(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];
}
모든 것이 맞습니다. 응용 프로그램이 백그라운드에있을 때마다 알림 메시지가 표시됩니다.
하지만 내 문제는 백그라운드 작업의 수명이 10 분이므로 10 분 후에 알림이 표시되지 않습니다. 이런 이유로 나는 배경 작업을 beginBackgroundTaskWithExpirationHandler
에 다시 시작하지만 내 응용 프로그램은 백그라운드 작업을 다시 시작하는이 시점에서 종료합니다.
응용 프로그램이 백그라운드 일 때 알림을 10 분 이상 사용할 수 없습니다.
제발 아무도 도와주세요.