如何在 iOS 上使用基于地理位置的推送通知? [英] How to use geo-based push notifications on iOS?

查看:31
本文介绍了如何在 iOS 上使用基于地理位置的推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序被终止时(不是在后台),是否可以在 iOS 上使用基于地理位置的推送通知?

Is it possible to make use of geo-based push notifications on iOS when the application is killed (not in the background)?

我有兴趣构建一个应用程序,用户将在其中选择地图上的位置,然后如果他/她靠近该区域,则会触发基于本地地理的推送通知.

I am interested in building an app, where the user will choose a position on a map, and then if he/she for example is close to that area a local geo-based push notification would be triggered.

然而,这个想法"甚至可能吗?GPS可以在app被杀的时候跑和比较坐标,到位的时候跑通知用户吗?是否有我可以阅读的有关该主题的教程/文章/更多信息?

However is this "idea" even possible? Can the GPS run and compare coordinates when the app is killed and run and notify the user when is in place? Is there a tutorial/article/more information of any kind on the subject that i could read?

我在网上阅读的大部分信息更像是实施的一般想法,但没有任何具体的内容.

Most of the information I read online were more like general ideas of implementing without anything specific though on the matter.

推荐答案

要在应用未运行(即之前已终止)时跟踪用户的位置,有两个选项:

For tracking a user's location while the app is not running (ie. has previously been terminated), there are two options:

  1. 来自 iOS 应用程序编程指南 在跟踪用户位置"下:

  1. From the iOS app programming guide under "Tracking the User's Location":

对于不需要高精度位置数据的应用,强烈建议使用显着变化位置服务.使用此服务,仅当用户位置发生显着变化时才会生成位置更新;因此,它非常适合社交应用程序或为用户提供非关键位置相关信息的应用程序.如果应用在发生更新时挂起,系统会在后台唤醒它以处理更新.如果应用启动此服务然后终止,系统会在新位置可用时自动重新启动应用.此服务适用于 iOS 4 及更高版本,并且仅适用于包含蜂窝无线电的设备.

The significant-change location service is highly recommended for apps that do not need high-precision location data. With this service, location updates are generated only when the user’s location changes significantly; thus, it is ideal for social apps or apps that provide the user with noncritical, location-relevant information. If the app is suspended when an update occurs, the system wakes it up in the background to handle the update. If the app starts this service and is then terminated, the system relaunches the app automatically when a new location becomes available. This service is available in iOS 4 and later, and it is available only on devices that contain a cellular radio.

但是,根据 CLLocationManager 类参考,它不太准确,更新很少:

However, according to the CLLocationManager class reference, it's not too accurate and updates are infrequent:

注意:只要设备从之前的通知移动 500 米或更远,应用就会收到通知.不应期望通知频率超过每五分钟一次.如果设备能够从网络检索数据,位置管理器就更有可能及时发送通知.

Note: Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

  • 区域监控 以类似的方式工作 - 包括在终止后重新启动应用程序 - 但精度更高(取决于 Wifi 网络和手机信号塔的可用性):

  • Region Monitoring works in a similar way - including restarting the app after being terminated - but with higher accuracy (depending on availability of Wifi networks and cell towers):

    具体的阈值距离由硬件和当前可用的定位技术决定.例如,如果禁用 Wi-Fi,区域监控的准确度就会大大降低.但是,出于测试目的,您可以假设最小距离约为 200 米.

    The specific threshold distances are determined by the hardware and the location technologies that are currently available. For example, if Wi-Fi is disabled, region monitoring is significantly less accurate. However, for testing purposes, you can assume that the minimum distance is approximately 200 meters.

    另一个区域监控注意事项是(根据CLLocationManager 类参考) 区域进入和退出通知可能仅在跨越区域边界后 3-5 分钟左右收到.

    Another region monitoring consideration is that (according to the CLLocationManager class reference) region entry and exit notifications might only be received 3-5 minutes or so after crossing the region's boundaries.

    根据实际需求,可以使用区域监控来获取粗略"位置,然后当用户在特定区域内时,在位置管理器上启动更准确的基于 GPS 的服务.当用户离开感兴趣的区域时,关闭GPS服务以节省电池并再次恢复到粗略位置监控服务(即区域监控).这是一个基本的实现:

    Depending on the actual requirements, region monitoring could be used for obtaining a "rough" location and then when the user is within a specific region, start up the more accurate GPS based service on the location manager. When the user leaves the region of interest, turn off the GPS service to preserve battery and revert to the rough location monitoring service (ie. region monitoring) once again. Here's a basic implementation:

    SomeViewController.m:

    ...
    @interface SomeViewController () <CLLocationManagerDelegate>
    
    @property (nonatomic, strong) CLLocationManager *locationManager;
    @property (nonatomic, strong) CLRegion *someRegion;
    
    @end
    
    @implementation SomeViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.locationManager = [[CLLocationManager alloc] init];
    
        CLLocationDistance radius = 10; // 10 metre sensitivity
        self.someRegion = [[CLRegion alloc] initCircularRegionWithCenter:someCoordinates radius:radius identifier:@"Smithtown Dry Cleaners"];
    
        self.locationManager.delegate = self;
        [self.locationManager startMonitoringForRegion:self.someRegion];
    
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = 10;
        [self.locationManager startUpdatingLocation];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
    {
        [self.locationManager startUpdatingLocation];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
    {
        [self.locationManager stopUpdatingLocation];
    }
    
    // Delegate method from the CLLocationManagerDelegate protocol.
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        CLLocation* location = [locations lastObject];
    
        // If the user's current location is not within the region anymore, stop updating
        if ([self.someRegion containsCoordinate:location.coordinate] == NO) {
            [self.locationManager stopUpdatingLocation];
        }
    
        NSString *locationData = [NSString stringWithFormat:@"latitude %+.6f, longitude %+.6f\n",
                                  location.coordinate.latitude,
                                  location.coordinate.longitude];
        NSLog(@"%@", locationData);
    
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.alertBody = locationData;
        localNotification.alertAction = @"Location data received";
        localNotification.hasAction = YES;
        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    }
    

    记住将适当的条目添加到应用程序的 plist 文件中,以便应用程序可以在后台运行并访问适当的资源:

    Remember to add the appropriate entries to the application's plist file so the app will run in the background with access to the appropriate resources:

    MyApp-Info.plist:

    <key>UIBackgroundModes</key>
    <array>
            ...
            <string>location</string>
    </array>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
            ...
            <string>location-services</string>
            <string>gps</string>
    </array>
    

    以上代码假设使用iOS6和ARC

    这篇关于如何在 iOS 上使用基于地理位置的推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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