john13th
john13th

Reputation: 29

Local notification to poll server at interval (iOS 7)

I have an NSTimer in an iOS that is polling a database server at every 10th seconds for a data row in a table based upon a certain data ID, that has been sent as an argument via a PHP-script. If the data ID matches the data ID of the row that have been inserted by an external source then the app will show an alert box containing the information from the data row and the NSTimer will stop to tick.

But this only works while the app is running in the foreground and I want to show the information message as a local notification so that even though the user has exited from the app, it will still poll the server when the app is running in the background as well.

I have read on many sites that the Local Notification service and background fetch is the right kind of solution but I don't know how to set it up really, it is very confusing. Because I have seen many examples where Local Notification is used to send reminders at certain dates on the calendar and trigger alarms at certain times and not so much about polling to a server.

How do you set up a Local Notification that will poll to a server at the interval of 10 seconds and then cancel it as soon as it receives right kind of information that it will display at last?

Here is how I have done so far:

...

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];
}

}

Upvotes: 0

Views: 899

Answers (1)

Paulw11
Paulw11

Reputation: 114866

You have it slightly backwards. The local notification doesn't check the server. Rather you implement background fetch and then post a local notification if the background fetch detects the relevant data. There is a good tutorial on background fetch here.

Note that background fetch won't execute every 10 seconds

Upvotes: 1

Related Questions