StartUpdatingLocation LocationManager via silent push notification

I am creating an application that needs to wake up in the background at a specific time.

I tried:

  • UILocalNotification: But I do not want to use UILocalNotification because it needs to interact with the user to use the notification, and only the application will wake up and launch the location manager.

    / li>
  • I also used [locationManager startUpdatingLocation];Modes Location with the background settings, this works, but it will take a lot of battery.

So, using the new iOS 7 method Silent pushNotificationand didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:, I need to run the location manager in the 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 works correctly,

Payload for PushNotification:

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

. , .

EDIT:

, , .

+1
4

Silent Pushnotification startUpdatingLocation, :

:

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

"": 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]];
}
+3

. , . .

, , , , ...

+1

, , . , . , , . , , , .

+1

, UIBackgroundModes plist. . startUpdatingLocation description apple doc.

, , ( ). , . , , UIBackgroundModes ( ) Info.plist.

xcode5 +, .

enter image description here

0

All Articles