問題描述
當應用程序被殺死(不是在后臺)時,是否可以在 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:
來自 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 上使用基于地理的推送通知?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!