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

标签 iphone ios geolocation push-notification apple-push-notifications

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

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

然而,这个“想法”有可能吗?当应用程序被杀死并运行并在到位时通知用户时,GPS 可以运行并比较坐标吗?是否有我可以阅读的有关该主题的任何类型的教程/文章/更多信息?

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

最佳答案

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

  1. 来自 iOS app programming guide在“跟踪用户位置”下:

    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 class reference , 它不太准确并且更新不频繁:

    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.

  2. Region Monitoring以类似的方式工作 - 包括在终止后重新启动应用程序 - 但准确性更高(取决于 Wifi 网络和手机信号塔的可用性):

    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 class reference )区域进入和退出通知可能仅在跨越区域边界后 3-5 分钟左右收到。

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

    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 文件中,以便应用程序在后台运行并访问适当的资源:

    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

关于iphone - 如何在 iOS 上使用基于地理的推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15590655/

相关文章:

iphone - 单击时突出显示表格 View 单元格,而不是在释放时突出显示

iphone - 自定义图像选择器/UIScrollView 未出现在 View 中

ios - 在 uibutton 上快速执行 2 个或多个控制事件

javascript - 根据地理位置获取最近距离

iphone - UIPopoverController、UIActionSheet 和模态视图 Controller 的保留/ Release模式?

iphone - iOS:检测WIFI是否通过认证?

ios - 获取 UICollectionView 部分的框架

ios - 如何用 react native 编写的新应用程序替换应用商店中的 ios native 应用程序

android - Firebase GeoFire,检索从我当前位置到特定半径的帖子

PHP 地理定位(街道地址 -> 经纬度)