2014-10-09 76 views
0

我在iOS中有一個NSTimer,它根據某個數據ID每隔10秒輪詢數據庫中的某個數據行,通過PHP腳本的參數。如果數據ID與已由外部源插入的行的數據ID相匹配,則應用程序將顯示包含來自數據行的信息的警報框,並且NSTimer將停止打勾。間隔(iOS 7)本地通知輪詢服務器

但是,這隻適用於應用程序在前臺運行時,我想顯示信息消息作爲本地通知,以便即使用戶已退出應用程序,它仍然會在應用程序是在後臺運行。

我已經閱讀了許多網站上的本地通知服務和後臺提取是正確的解決方案,但我不知道如何設置它真的很混亂。 因爲我看到很多例子,其中本地通知用於在日曆上的特定日期發送提醒,並在特定時間觸發警報,而不是在輪詢服務器。

如何設置一個本地通知,以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

稍微有點後退。本地通知不檢查服務器。相反,如果後臺獲取檢測到相關數據,則實施後臺提取併發布本地通知。這裏是一個關於後臺抓取的教程 - http://www.appcoda.com/ios7-background-fetch-programming/請注意,後臺抓取不會每10秒執行一次 – Paulw11 2014-10-09 01:34:58

+0

感謝您的幫助!我按照您提交的鏈接上的指示進行操作,並根據您的帖子進行實施,通過後臺提取併發布相關數據的本地通知。它的工作原理完全按照我的意願:) – john13th 2014-10-16 23:24:13

回答

1

你稍微向後。本地通知不檢查服務器。相反,如果後臺獲取檢測到相關數據,則實施後臺提取併發布本地通知。有一個很好的背景獲取教程here

請注意,後臺提取將不會每10秒執行一次