title: WebServices Call when application in Background and show the response in Local Notification
body: <p>I have an enterprise application that I want to keep running, so it can call a webservice and inform to the user through Local Notification.</p>
  
  <p>So, it now runs in the background, it makes the calls, gets results, informing to the user.
  I Used a timer in applicationDidEnterBackground. My Code is,</p>
  
  <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application
  {
      // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
      // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
      NSLog(@"ENTER BACKGROUND");
      if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground)
      {
          backgroundTimer=nil;
          [backgroundTimer invalidate];
          backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self  selector:@selector(backgroundTasks) userInfo:nil repeats:YES];
      }
  
      application.applicationIconBadgeNumber = 0;
  }
  </code></pre>
  
  <p>When the timer is triggered, i will check the date condition. If Date is matched then i have to make webservice call.</p>
  
  <pre><code>-(void)backgroundTasks
  {
      NSDate *CurrentDate = [NSDate date];
      NSDate *previousDate = [[NSUserDefaults standardUserDefaults]valueForKey:@"ScheduledTime"];
  
      NSLog(@"Current Date : %@ - Previous Date : %@",CurrentDate,previousDate);
      NSString *CurrentdateString = [NSString stringWithFormat:@"%@",CurrentDate];
      NSString *PreviousdateString = [NSString stringWithFormat:@"%@",previousDate];
      NSLog(@"Current Date String : %@ - Previous Date String : %@",CurrentdateString,PreviousdateString);
  
      if ([PreviousdateString isEqualToString:CurrentdateString])
      {
          NSLog(@"Both dates are same");
          previousDate = [previousDate dateByAddingTimeInterval:60];
          [[NSUserDefaults standardUserDefaults] setObject:previousDate forKey:@"ScheduledTime"];
          [[NSUserDefaults standardUserDefaults] synchronize];
  
          [self updateUserLatLong:str_Latitude :str_Longitude]; //Webservices call
      }
      else
      {
          NSLog(@"Dates are different");
      }
  }
  </code></pre>
  
  <p>I got the response from this request, at that time i will create Local Notification. like this,</p>
  
  <pre><code> str_Condition = [NSString stringWithFormat:@"%@",[conditionarray lastObject]];
                          str_TempFaren = [NSString stringWithFormat:@"%@",[mutArr_High lastObject]];
                          NSLog(@"Condition : %@, Temperature : %@",str_Condition,str_TempFaren);
  
                          NSLog(@"SHOW ALERT NOTIFICATION");
                          NSLog(@"-----------------------------------------------------------------------");
  
  
                          UILocalNotification *localNotif = [[UILocalNotification alloc] init];
                          localNotif.fireDate = [NSDate date];
                          localNotif.timeZone = [NSTimeZone defaultTimeZone];
                          localNotif.alertBody = [NSString stringWithFormat:@"%@° F - %@, See today's recommendations",str_TempFaren,str_Condition];
                          localNotif.alertAction = @"Reminder";
  
                          localNotif.soundName = UILocalNotificationDefaultSoundName;
                          localNotif.applicationIconBadgeNumber =0;
                          [[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
  </code></pre>
  
  <p>I Create Default Notification in another class, like this.</p>
  
  <pre><code> pickerDate = [datePicker date];
      // Schedule the notification
      UILocalNotification* localNotification = [[UILocalNotification alloc] init];
      localNotification.fireDate = pickerDate;
      localNotification.alertBody = @"See today's recommendations";
      localNotification.alertAction = @"Reminder";
      localNotification.timeZone = [NSTimeZone defaultTimeZone];
      localNotification.repeatInterval = NSMinuteCalendarUnit;
  
  //    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
      localNotification.soundName = UILocalNotificationDefaultSoundName;
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
  </code></pre>
  
  <p>My Problem is, when the user remove the application from memory, obviously we can't make any webservices call. So when the app is removed from the memory, i want to show the message in localnotification which contains default alertbody. Now Both scenarios are working. but i want to display only one notification. if app removed from memory then only i have to show default notification. How to handle this problem, Please help me. i was strucked more than 7 hours for this issue.</p>