24

當應用程序被終止(不在後臺)時,是否可以在iOS上使用基於地理位置的推送通知?如何在iOS上使用基於地理位置的推送通知?

我有興趣創建一個應用程序,用戶可以在地圖上選擇一個位置,然後如果他/她靠近該區域,則會觸發本地基於地理位置的推送通知。

但是這個「想法」甚至可能嗎?當應用程序被終止並運行時,GPS是否可以運行並比較座標,並在何時到位時通知用戶?是否有關於我可以閱讀的主題的任何類型的教程/文章/更多信息?

我在網上閱讀的大部分信息更像是一般性思想,沒有任何具體的實現。

+0

的爲什麼你不希望在後臺運行的應用程序(應用程序殺死後指)?我在我的應用程序中使用了與後臺運行功能相同的概念。你能詳細說明一下嗎?例如, – 2013-03-23 19:57:18

+0

。讓我們說用戶提醒,他想去看他的祖母時看到它。這可能是下週。肯定電話可能會被重置,或者他可能已經從後臺刪除了應用程序。所以即使應用沒有在後臺運行,我也需要推動它。你能告訴我的應用程序的名稱,我真的很感興趣,看看你已經實現:) – donparalias 2013-03-23 20:38:59

+0

1.當應用程序死亡不可能時,GPS沒有運行。 2.當您評論一個示例時,您可以從CMS端對提醒時間設置推送通知。在網絡服務上,您可以設計一種算法,通過移動提醒設置發送推送通知。 – 2013-03-24 07:03:24

回答

35

用於跟蹤用戶的位置,而應用程序沒有運行(即先前已終止),有兩種選擇:

  1. iOS app programming guide「跟蹤用戶的位置」下:

    強烈建議對不需要高精度位置數據的應用程序進行重大更改的位置服務。有了這項服務,只有當用戶的位置發生重大變化時纔會生成位置更新;因此,它非常適合向用戶提供非關鍵的位置相關信息的社交應用或應用。如果應用程序在發生更新時暫停,系統會在後臺將其喚醒以處理更新。 如果應用程序啓動此服務,然後終止,系統會在新位置可用時自動重新啓動應用程序。此服務在iOS 4及更高版本中可用,並且僅在包含蜂窩無線電的設備上可用。

    然而,根據CLLocationManager class reference,這不是太準確,更新不頻繁:

    注:應用程序能夠儘快期望通知,該設備從之前通知移動500米以上。它不應該比每五分鐘更頻繁地發出通知。如果設備能夠從網絡中檢索數據,位置管理器更有可能及時發送通知。

  2. Region Monitoring作品以類似的方式 - 包括重新啓動被終止後的應用程序 - 但以更高的精度(取決於WiFi網絡和蜂窩塔的情況而定):

    的具體閾值距離確定通過當前可用的硬件和位置技術。例如,如果Wi-Fi已禁用,則區域監控的精確度會大大降低。但是,出於測試目的,您可以假定最小距離大約爲200米。

    另一個區域監測考慮是(根據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

+0

是的,它看起來確實如此!儘管我們在這種情況下談論什麼精度,你有什麼想法嗎?因爲我見過一些應用程序(沒有下載它們,因爲它們很昂貴),這將支持地理推送,但精度爲500米左右。這對我的目的來說太過分了。我不知道你是否可以選擇大約50-100米的精度,然而它總是需要多少電池才能運行。我現在更多地瞭解它 – donparalias 2013-03-23 22:21:41

+0

由於重大變更的位置服務依賴於設備更換單元塔,因此可能不夠好。 – gavdotnet 2013-03-23 22:32:22

+0

但是,應該可以監聽將應用程序喚醒(如果已終止)的「重大更改」通知,然後開始使用標準的後臺位置服務 - 這會提供更好的準確性 - 直到應用程序再次終止。當應用程序未運行時,您當然會失去一些準確性,但其餘時間與任何後臺應用程序一樣準確。 – gavdotnet 2013-03-23 22:45:50