![](/img/trans.png)
[英]How to achieve ios local push notification based on user location without running app in the background and draining battery?
[英]location based push notification when app is not running?
是否可以在您的應用未運行但用戶仍然能夠在他靠近特定位置時收到推送通知。 這將需要檢查用戶當前的緯度和經度(即使應用程序未運行)。
如果有可能,您能否就如何實現它提供一些指導?
即使應用程序未運行,您也可以使用兩種方法來跟蹤用戶位置:
重要的位置變更服務。
在CLLocationManager實例中調用方法startMonitoringSignificantLocationChanges
。 這將有助於顯着節省功率,但與下面的第二種方法相比,它不能提供高精度。
你並不需要設置location
鍵UIBackgroundModes
,如果你選擇這種方法。
標准位置服務。
在CLLocationManager實例中調用方法startUpdatingLocation
。 這需要大量的設備功率,但它提供更高的精度。 請記住在UIBackgroundModes
設置location
鍵,以確保即使應用程序被UIBackgroundModes
,跟蹤仍然有效。
您可以使用上述任一方法之一。 我使用第一種方法結合區域監控來觸發本地通知。
您可以查看以下Apple文檔,以獲得更詳盡的解釋和示例:
基於區域(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的更多細節。
你需要做兩件事:
但是,用戶仍然可以將其關閉(設置 - >常規 - >后台應用程序刷新)。
你可以在應用程序處於后台時執行此操作。 首先..Call CLLocationManager會在應用程序轉到后台時刪除。 每當設備獲得新的緯度和經度時..此刪除將被解雇。 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations; 現在調用Web服務並更新您當前的位置。 然后發送推送通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.