iOS应用背景位置(推送通知) [英] iOS App Background Location (Push Notifications)

查看:102
本文介绍了iOS应用背景位置(推送通知)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,当我的应用程序在后台运行时,当他们收到推送通知时,我的应用程序要求用户提供位置信息。

I face an issue where my app requires the users location when they receive a push notification while the app is running in the background.

经过一点阅读之后我觉得这是不可能的,即:

After doing a bit of reading I'm under the impression that this isn't possible, i.e:

1)后台应用

2)收到推送通知

3)获取用户位置并执行代码块。

3) Get users location and execute block of code.

我发现了这个< a href = https://stackoverflow.com/questions/6347503/how-do-i-get-a-background-location-update-every-n-minutes-in-my-ios-application>在此处发布表示您可以每N分钟运行一次任务来获取用户位置。因此,我可以每十分钟检查一次,这样可以正常工作,但是如果我继续激活GPS,将会浪费用户的电池。因此,我想知道我是否可以在后台任务中判断用户是否有任何推送通知在等待我的应用程序?这样,我将检查它们是否每隔十分钟执行一次,并且仅在激活时才激活GPS。

I found this post on here that suggests you can run a task every N amount of minutes to get the users location. So using this I could check say every ten minutes which would work but it would be a waste of the users battery if I kept activating the GPS. Therefore I was wondering can I tell within my background task if the user has any push notification waiting for my app? That way I would check if they did every ten minutes and only activate the GPS if they did.

也许我会用所有错误的方式,我真的会感谢任何建议。很抱歉,您还没有后台任务的任何代码,因为我仍在尝试使其工作。

Perhaps I'm going about this all the wrong way, I would really appreciate any advice. Sorry for not having any of the code for the background task yet as I'm still trying to get it to work.

[UPDATE]

感谢达斯汀的建议。经过一些测试,我决定选择一个更简单的选项。我现在正在使用Apple重要位置更改。我将更改存储在一个数组中,并将其设置为仅在上一次更改大于10分钟前才更新。然后,当应用程序激活时,我将数组中与发送推送通知的时间最接近的时间匹配并使用该位置。

Thanks Dustin for your advice. After a bit of testing I've decided to go with an easier option. I am now using the Apple Significant Location Changes. I store the changes in an array, I've set this to only update if the last change was greater than 10min ago. Then when the app becomes active I match the closest time in the array to the time the push notification was sent and use that location.

推荐答案

正如我讨论过的,使用替代解决方案,这里是:

As discussed I used alternative solution, here it is:

在viewDidLoad或didFinishingLaunchingWithOptions下添加:

Under viewDidLoad or didFinishingLaunchingWithOptions add:

locationsLogged = [[NSMutableArray alloc] init];
lastLocationTime = [[NSDate alloc] init]
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

根据应用程序DidEnterBackground添加:

Under applicationDidEnterBackground add:

[locationManager startMonitoringSignificantLocationChanges];

添加:

-(void) storeLocations:(CLLocation *)location
{
bgTask = [[UIApplication sharedApplication]
          beginBackgroundTaskWithExpirationHandler:
          ^{
              [[UIApplication sharedApplication] endBackgroundTask:bgTask];
               }];

[locationsLogged addObject:location];

if (bgTask != UIBackgroundTaskInvalid)
{
    [[UIApplication sharedApplication] endBackgroundTask:bgTask];
     bgTask = UIBackgroundTaskInvalid;
     }
}


 -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
    isInBackground = YES;
} 


NSDate *now = [NSDate date];
NSTimeInterval diff = [now timeIntervalSinceDate:lastLocationTime];


if ((int)diff > 600) {

    if (isInBackground)
    {
        [lastLocationTime release];
        lastLocationTime = [[NSDate alloc] init];
        [self storeLocations:newLocation];
    }
    else
    {
        // ...
    }
}
}

然后在applicationDidBecomeActive上,您可以使用需要的任何逻辑来应用位置信息。

Then on applicationDidBecomeActive you can use whatever logic you need to apply location information.

这篇关于iOS应用背景位置(推送通知)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆