سؤال

my application is using google maps , and its showing a direction for a car so the user can see it moving on map , my issue is that: 1. when the user put the application in background i want the NSTimer to keep running 2. while its in background and when i receive the response from api that the car is arrived ,, i want to send a local notification so the user can see it and open the app

here is some of the code:

//here is the timer 
searchTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(getLocationForDriver) userInfo:Nil repeats:YES];

-(void)getLocationForDriver
{
NSArray* pks = [NSArray arrayWithObject:self.driverPK];

[[NetworkEngine getInstance] trackBooking:pks completionBlock:^(NSObject* response)
 {

     NSDictionary* dict = (NSDictionary*)response;
     currentStatus = [dict objectForKey:@"current_status"];

.
.
.
.
   if ([currentStatus isEqualToString:@"completed"])
     {
       //here i want to send the local notification
     }

 }
}
هل كانت مفيدة؟

المحلول

Usually when an app is sent to the background it is terminated shortly after by the system. The exception is an app that fits one of Apple's UIBackgroundModes (I suggest you read more about app states and multitasking, specifically the part about long running background tasks). This is a mechanism which allows apps that require running long tasks in the background to do so without being terminated (navigation apps, VoIP apps, music apps...)

Based on your question, it seems reasonable that your app would use the Location updates background mode. In order to enable this mode you need to go to your target -> Capabilities, turn Background Modes to on and check the Location updates box. Once you've done that your app won't be terminated once it is sent to the background and you should be able to run an NSTimer, get the api response and send a notification like your normally do.

Keep in mind that your app needs a good reason for using one of the background modes or it will be rejected from the app store.

UPDATE

In order to send a local notification you can add the following lines to your code:

if ([currentStatus isEqualToString:@"completed"])
     {
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = [NSDate date];
        localNotification.alertBody = @"My notification text";
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
       [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
     }

This sends a local notification immediately and adds +1 to the app's icon badge. (You can read more about UILocalNotification here and here) If your app is running in the background when the notification is fired then the user will see the notification as a banner on the top of the screen.

You can then handle the notification by implementing the application delegate's application:didReceiveLocalNotification: method. Keep in mind that if your app is running in the background this method will only get called once the user brings the app to the foreground by either clicking on the notification banner or the app icon.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top