startUpdatingLocation of LocationManager through silent push notification

巧了我就是萌 提交于 2019-12-03 00:52:06

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:

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

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..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!