【问题标题】:Local notification to poll server at interval (iOS 7)定期轮询服务器的本地通知(iOS 7)
【发布时间】:2014-10-09 00:03:26
【问题描述】:

我在 iOS 中有一个 NSTimer,它每隔 10 秒轮询一次数据库服务器,以获取基于某个数据 ID 的表中的数据行,该数据 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];
}

}

【问题讨论】:

  • 你有点倒退了。本地通知不检查服务器。相反,您实现后台获取,然后在后台获取检测到相关数据时发布本地通知。这是关于后台获取的教程 - appcoda.com/ios7-background-fetch-programming 请注意,后台获取不会每 10 秒执行一次
  • 感谢您的帮助!我按照您提交的链接上的说明进行操作,并根据您的帖子进行了实施,通过后台获取并发布相关数据的本地通知。它完全符合我的要求:)

标签: ios background notifications nstimer polling


【解决方案1】:

你有一点倒退。本地通知不检查服务器。相反,您实现后台提取,然后在后台提取检测到相关数据时发布本地通知。有一个很好的后台获取教程here

请注意,后台获取不会每 10 秒执行一次

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 2019-11-27
    • 1970-01-01
    相关资源
    最近更新 更多