在didFinishLaunchingWithOptions
我每 30 秒后向服务器发出请求以获取Y
或N
使用NSTimer
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSTimer *notifyTimer = [NSTimer timerWithTimeInterval:30.0 target:self selector:@selector(httpRequest) userInfo:nil repeats:YES];//7200.0
[NSRunLoop mainRunLoop] addTimer:notifyTimer forMode:NSDefaultRunLoopMode];
return YES;
}
-(NSString *)httpRequest {
NSURL *url = [NSURL URLWithString:@"http:192.168.10.67/t.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[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];
}
}
如果响应是Y
我正在注册一个UILocalNotification
将在一秒钟内触发的。
-(void)showLocalNotification {
NSLog("Notification is here.");
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];
}
UILocalNotification
当应用程序在后台时,不向用户显示通知警报并且不打印日志。
当应用程序回到前台时,它会立即打印所有日志语句。我究竟做错了什么?
此外,它是一个 VoIP 应用程序。并且 UIBackgroundMode 具有 voip 和音频集。