如何在 iOS 上使用基于地理的推送通知?

How to use geo-based push notifications on iOS?(如何在 iOS 上使用基于地理的推送通知?)

本文介绍了如何在 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 是否可以在应用程序被杀死并运行并在到位时通知用户时运行并比较坐标?是否有我可以阅读的关于该主题的任何类型的教程/文章/更多信息?

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 应用编程指南在跟踪用户位置"下:

对于不需要高精度位置数据的应用,强烈建议使用重大变化的位置服务.使用此服务,仅当用户的位置发生重大变化时才会生成位置更新;因此,它非常适合社交应用程序或为用户提供非关键、位置相关信息的应用程序.如果应用程序在发生更新时被挂起,系统会在后台将其唤醒以处理更新.如果应用启动此服务然后终止,系统会在新位置可用时自动重新启动应用.此服务在 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
    ",
                                  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 上使用基于地理的推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

  • 本文标题为:如何在 iOS 上使用基于地理的推送通知?

    基础教程推荐