我在iOS中有一个NSTimer,它根据某个数据ID在表格中每10秒轮询一次数据库服务器,该数据ID已经通过PHP脚本作为参数发送。如果数据ID与外部源插入的行的数据ID匹配,则应用程序将显示一个警告框,其中包含数据行中的信息,NSTimer将停止勾选。
但这只适用于应用程序在前台运行时我想将信息消息显示为本地通知,这样即使用户已退出应用程序,它仍会在应用程序运行时轮询服务器在后台也是。
我已经在许多网站上看到本地通知服务和后台提取是正确的解决方案,但我不知道如何设置它,这是非常令人困惑的。 因为我看过很多例子,其中使用本地通知在日历上的某些日期发送提醒,并在特定时间触发警报,而不是轮询到服务器。
如何设置本地通知,以10秒的间隔轮询到服务器,然后在收到最后显示的正确信息后立即取消?
到目前为止我的表现如下:
...
NSTimer *confirmedTimer;
int orderId = 1;
...
-(IBAction) sendButton: (id) sender {
confirmedTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(confirmedTick) userInfo:nil repeats:YES];
}
-(void)confirmedTick {
NSString *paramsConfirmed = [NSString stringWithFormat:@"order_id=%d", orderId];
NSData *postDataConfirmed = [paramsConfirmed dataUsingEncoding:NSUTF8StringEncoding];
NSURL *urlConfirmed = [NSURL URLWithString:@"http://www.serverexample.com/confirmed.php"];
NSMutableURLRequest *requestConfirmed = [NSMutableURLRequest requestWithURL:urlConfirmed];
[requestConfirmed setHTTPMethod:@"POST"];
[requestConfirmed addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[requestConfirmed setHTTPBody:postDataConfirmed];
[requestConfirmed setValue:[NSString stringWithFormat:@"%i", postDataConfirmed.length] forHTTPHeaderField:@"Content-Length"];
NSURLResponse *responseConfirmed;
NSError *errorConfirmed = nil;
NSData *receivedDataConfirmed = [NSURLConnection sendSynchronousRequest:requestConfirmed
returningResponse:&responseConfirmed
error:&errorConfirmed];
if(errorConfirmed) {
if([responseConfirmed isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponseConfirmed = (NSHTTPURLResponse *)responseConfirmed;
return;
}
return;
}
NSString *responseStringConfirmed = [[NSString alloc] initWithData:receivedDataConfirmed
encoding:NSUTF8StringEncoding];
if ([responseStringConfirmed isEqualToString:@"true"]) {
return;
}
NSDictionary *jsonObjectConfirmed = [responseStringConfirmed objectFromJSONString];
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:receivedDataConfirmed options:0 error:nil];
NSArray *confirmedArray = [jsonDictionary objectForKey:@"confirmed_table"];
if([confirmedArray count] > 0)
{
[confirmedTimer invalidate];
NSString *confirmedMessage = @"";
for(NSDictionary *confirmed in confirmedArray)
{
confirmedMessage = [confirmedMessage stringByAppendingString:[NSString stringWithFormat:@"confirmed_id: %@\n", [NSNumber numberWithInt:[[confirmed objectForKey:@"confirmed_id"] intValue]]]];
confirmedMessage = [confirmedMessage stringByAppendingString:[NSString stringWithFormat:@"order_id: %@\n", [NSNumber numberWithInt:[[confirmed objectForKey:@"order_id"] intValue]]]];
confirmedMessage = [confirmedMessage stringByAppendingString:[NSString stringWithFormat:@"Information: %@", [confirmed objectForKey:@"information"]]];
}
UIAlertView *confirmedAlert = [[UIAlertView alloc]
initWithTitle:@"Confirmation"
message:confirmedMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[confirmedAlert show];
[confirmedAlert release];
}
}
答案 0 :(得分:1)
你略微倒退了。本地通知不检查服务器。而是实现后台提取,然后在后台提取检测到相关数据时发布本地通知。有一个很好的背景提取教程here。
请注意,后台提取不会每10秒执行一次