startUpdatingLocation of LocationManager through silent push notification

后端 未结 4 996
南笙
南笙 2021-02-04 22:39

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 UILoca

相关标签:
4条回答
  • 2021-02-04 23:23

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

    0 讨论(0)
  • 2021-02-04 23:26

    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

    0 讨论(0)
  • 2021-02-04 23:28

    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]];
    }
    
    0 讨论(0)
  • 2021-02-04 23:32

    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.

    0 讨论(0)
提交回复
热议问题