【问题标题】:location based push notification when app is not running?应用程序未运行时基于位置的推送通知?
【发布时间】:2013-04-01 04:01:05
【问题描述】:
当您的应用未运行但用户在特定位置附近时仍然能够收到推送通知时,是否有可能。这将需要检查用户当前的纬度和经度(即使应用程序没有运行)。
如果可能的话,您能否就如何实现它提供一些指导?
【问题讨论】:
标签:
ios
ios5
location
apple-push-notifications
【解决方案1】:
【讨论】:
-
标准位置服务事件不会重新启动应用程序,如果它被杀死。 The standard location service delivers events normally while an app is running in the foreground. When your app is in the background, this service delivers events only when the location-updates background mode is enabled for the app. This service does not relaunch iOS apps that have been terminated.取自here
【解决方案2】:
基于区域(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的详细信息。
【解决方案3】:
你需要做两件事:
- 使用 CLLocationManager 启动MonitoringSignificantLocationChanges。那是在代码方面。您可能想收听通知,并使用您获得的信息做任何事情。但是,如果应用没有运行,除非您执行以下两项操作,否则您将无法获得此信息:
- 在功能中:启用背景模式和模式:位置更新。这样一来,我相信即使您的应用从非活动列表中滑落,您也会始终收到通知。
但是,用户仍然可以将其关闭(设置->常规->后台应用刷新)。
【解决方案4】:
您可以在应用程序处于后台时执行此操作。
首先..每当应用程序进入后台时调用 CLLocationManager deleagte。
每当设备将获得新的纬度和经度.. 这个委托将被解雇。
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
现在调用网络服务并更新您的当前位置。
然后发送推送通知。