簡體   English   中英

應用未運行時基於位置的推送通知?

[英]location based push notification when app is not running?

是否可以在您的應用未運行但用戶仍然能夠在他靠近特定位置時收到推送通知。 這將需要檢查用戶當前的緯度和經度(即使應用程序未運行)。

如果有可能,您能否就如何實現它提供一些指導?

即使應用程序未運行,您也可以使用兩種方法來跟蹤用戶位置:

  1. 重要的位置變更服務。

    在CLLocationManager實例中調用方法startMonitoringSignificantLocationChanges 這將有助於顯着節省功率,但與下面的第二種方法相比,它不能提供高精度。

    並不需要設置locationUIBackgroundModes ,如果你選擇這種方法。

  2. 標准位置服務。

    在CLLocationManager實例中調用方法startUpdatingLocation 這需要大量的設備功率,但它提供更高的精度。 請記住在UIBackgroundModes設置location鍵,以確保即使應用程序被UIBackgroundModes ,跟蹤仍然有效。

您可以使用上述任一方法之一。 我使用第一種方法結合區域監控來觸發本地通知。

您可以查看以下Apple文檔,以獲得更詳盡的解釋和示例:

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

基於區域(CLRegion)的通知即使應用程序根本沒有運行

嗯,這個答案不是一個快速的答案,但Apple在UILocalNotification中引入了新的概念。 發送推送通知不需要,當用戶進入/退出地理區域CLRegion時,iOS會自動顯示本地通知。 從iOS 8及更高版本開始,我們可以根據位置安排本地通知, 而不是通過設置fireDate屬性

    let localNotification = UILocalNotification()
    localNotification.alertTitle = "Hi there"
    localNotification.alertBody = "you are here"

    let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: 4.254, longitude: 88.25), radius: CLLocationDistance(100), identifier: "")
    region.notifyOnEntry = true
    region.notifyOnExit = false
    localNotification.region = region       

    localNotification.timeZone = NSTimeZone.localTimeZone()
    localNotification.soundName = UILocalNotificationDefaultSoundName

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

以下是Apple的更多細節。

你需要做兩件事:

  1. 使用CLLocationManager startMonitoringSignificantLocationChanges。 這是在代碼方面。 您可能希望收聽通知,並根據您獲得的信息執行任何操作。 但是,如果應用程序沒有運行,除非你做兩個,否則你不會得到這些信息:
  2. 在功能中:啟用BackgroundModes和模式:位置更新。 這樣,即使您的應用從非活動列表中刪除,我相信您也會收到通知。

但是,用戶仍然可以將其關閉(設置 - >常規 - >后台應用程序刷新)。

你可以在應用程序處於后台時執行此操作。 首先..Call CLLocationManager會在應用程序轉到后台時刪除。 每當設備獲得新的緯度和經度時..此刪除將被解雇。 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations; 現在調用Web服務並更新您當前的位置。 然后發送推送通知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM