In my app, I am planning to use Local Notification and not Push Notifications. I need to check whether a transaction update is available, if YES, just notify the user with Local Notification. I have done the below steps.
didFinishLaunchingWithOptions
, set fetch interval [[UIApplication sharedApplication]setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]
.-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
> Added code for POST Request as below with my request data:
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn) {
completionHandler(UIBackgroundFetchResultNewData);
}
else {
completionHandler(UIBackgroundFetchResultFailed);
}
After response parsing, comparing if any update is there and if any update is there, notification is displayed using the below code.
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertTitle = @"SIB Mirror";
localNotification.alertBody = @"You have a new transaction. Go to e-Statements to view it.";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
The issue is most of the times, when phone is locked, getting error message >Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
and hence notification was not getting displayed. But when I connect Phone directly to Phone and in debug mode enable background sync, every thing works properly.
Kindly guide me an easy approach to do this or correct me if anything I am doing wrongly.
Thanks,
Mobile Developer.
Try the below code:
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (error == nil)
{
// Parse data here
completionHandler(UIBackgroundFetchResultNewData);
}
else {
completionHandler(UIBackgroundFetchResultFailed);
}