iosobjective-cios7cllocationmanageruibackgroundtask

startUpdatingLocation of LocationManager through silent push notification


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.


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