Question

I am creating app that needs to wake up in background at particular time .

I have tried :

  1. UILocalNotification : But i Don't want to use UILocalNotification because it needs user interaction to tap on notification and than only app will wake up and start location manager.

  2. I have also used [locationManager startUpdatingLocation]; enabled with background Modes Location updates, this is works but it will take lot of battery.

So with using new iOS 7 feature Silent pushNotification and didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: method i need to start location manager in background,

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

    NSLog(@"Receive RemoteNotification in fetchCompletionHandler with UserInfo:%@",userInfo);
    application.applicationIconBadgeNumber=0;
    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{

        [self.locationManager startUpdatingLocation];

        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

}

Silent Push notification is working correctly ,

Payload for PushNotification:

{"aps" : {"content-available" : 1},"SilentPushId" : "07"}

But this will not start location manager , Please somebody help me.

EDIT:

If it is not possible please give me some suggestions.

Was it helpful?

Solution

I have successfully implement this Using Silent Pushnotification & it Call startUpdatingLocation and I am able to get location data in delegate method :

Using This Payload:

{
    "aps": {
        "content-available": 1
    },
    "SilentPush": "4"
}

I have enabled location & remote notification for Background mode: enter image description here

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
     __block UIBackgroundTaskIdentifier bgTask =0;
    UIApplication  *app = [UIApplication sharedApplication];
     bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [self.locationManager startUpdatingLocation];

 }];

didUpdateLocations

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
        lastLoc=[locations lastObject];
        [logFile addLocObject:[NSString stringWithFormat:@"Loc: %@",lastLoc]];
}

OTHER TIPS

You cannot. It is explicitly not permitted to start location services while the application is in the background. The user has to be aware of the start with the app open.

Imagine what subversive tracking would be available if you could send silent notifications to trigger a location update back to a server without the user knowing about it...

What you can do, in background, is receive significantLocation events and boundary events. I can imagine leveraging that capability to keep a recent locations log of sorts. When your remote notification is received, respond by sending last known location. With a bit of experimentation I am sure you could refine this to be reasonably accurate with little impact on the battery.

If you want to update location in background mode, you just enable UIBackgroundModes in plist file. See startUpdatingLocation description in apple's doc..

If you start this service and your application is suspended, the system stops the delivery of events until your application starts running again (either in the foreground or background). If your application is terminated, the delivery of new location events stops altogether. Therefore, if your application needs to receive location events while in the background, it must include the UIBackgroundModes key (with the location value) in its Info.plist file.

You can enable this for background updation in your xcode5+ as like below..

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top