2

大家晚上好 !

我有一个简单的问题:当我在 iOS 7 上使用 didReceiveRemoteNotification:fetch 收到正确的推送通知时,如何启用 [CLLocationManager startUpdateLocation]?

现在,我有:

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    if ([userInfo objectForKey:@"aps"] && [[userInfo objectForKey:@"aps"] objectForKey:@"content-available"])
    {
        if ([userInfo objectForKey:@"update-location"])
        {
            [self performSelectorInBackground:@selector(handleLocationNotificationPush:) withObject:completionHandler];
        }
        if ([userInfo objectForKey:@"update-sensors"])
        {

        }
    }
}

-(void)handleLocationNotificationPush:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [CLController.locMgr startUpdatingLocation];
    ++nbPushReceive;

    [self.pushLockForLocation lock];
    if ([self.pushLockForLocation waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:25]] == TRUE && self.lastKnownLocation != nil)
    {
        [self.pushLockForLocation unlock];
        // Send my new location to server using HTTP request
        [self sendLocationForPushUpdates:self.lastKnownLocation fetchCompletionHandler:completionHandler];

        if (![UIApplication sharedApplication].applicationState == UIApplicationStateActive)
            // it stops location updates
            [self stopAllLocationUpdates];
        return;
    }
    // In case we didn't receive any new position during 25 secondes
    [self.pushLockForLocation unlock];
    if (![UIApplication sharedApplication].applicationState == UIApplicationStateActive)
    {
        [self stopAllLocationUpdates];
    }
    completionHandler(UIBackgroundFetchResultNoData);
}


- (void)locationUpdate:(CLLocation *)location
{
    NSLog(@" *** LocationContrller - LocationUpdate location");

    self.lastKnownLocation = location;
    if (location.horizontalAccuracy < 500)
    {
        [self.pushLockForLocation signal];
    }
}

当然,我的 CLController 委托是同一个类(当应用程序处于前台时,位置更新工作)。在我的 plist.file 中,我检查了“远程通知”。

我错过了什么?

谢谢你的帮助 !:D

4

2 回答 2

2

这可能与在 iOS7 中在后台启用定位服务并不会像在以前的 iOS 版本中那样为您提供无限的后台处理时间有关。观看 WWDC 2013 What's New in Core Location 视频大约 5 分 30 秒。因此,您的应用程序在收到推送通知后大约 30 秒再次暂停。

我有一个类似的问题,但我还没有找到解决方案。但是,如果您想去同一个地方,我可以尝试以下方法 -

首先在 didReceiveRemoteNotification 中放置一个 NSLog,在设备上运行您的应用程序,将其置于后台并向其发送推送通知。如果您看到您的 NSLog,您就会知道在您的推送中正确设置了 content-available 标志。

接下来在 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 中添加一个 NSLog。如果这得到提示,那么您知道您确实在启用位置服务。

如果你走到这一步,你可能会遇到和我一样的问题。30 秒并不总是足够长以达到我想要的准确度。

顺便说一句,如果您使用的是 iPhone 5 或更新版本,您可以使用延迟定位并保持定位服务持续运行。可悲的是,我需要它在 iPhone 4s 上工作。

更新 - 我现在发现这与后台推送通知和 didReceiveRemoteNotification 密切相关。从标准后台任务启用位置管理器将像在 iOS7 之前一样工作。因此,您仍然可以将后台任务与位置管理器一起使用,每 x 分钟获取一次您的位置,您只是无法使用后台推送开始整个事情。

于 2013-10-18T14:47:53.497 回答
0

回答我自己,并使用几个stackoverflow的帖子,当你在后台时,显然不可能重新激活位置更新。为了不断更新您的位置更新,即使您在后台运行,您也必须让它运行!

:)

于 2013-10-28T09:47:07.487 回答