应用未运行时基于位置的推送通知?

时间:2013-04-01 04:01:06

标签: ios ios5 location apple-push-notifications

当您的应用未运行但用户在特定位置附近时仍然能够接收推送通知时,是否可能。这将需要检查用户当前的纬度和经度(即使应用程序未运行)。

如果有可能,你能否就如何实现它做一些指导?

4 个答案:

答案 0 :(得分:5)

即使应用未运行,您也可以使用两种方法来跟踪用户位置:

  1. 重要地点变更服务。

    在CLLocationManager实例中调用方法startMonitoringSignificantLocationChanges。这将有助于显着节省功率,但与下面的第二种方法相比,它不能提供高精度。

    如果您选择使用此方法,需要在location设置UIBackgroundModes密钥。

  2. 标准位置服务。

    在CLLocationManager实例中调用方法startUpdatingLocation。这需要大量的设备功率,但它提供更高的精度。请务必在location中设置UIBackgroundModes密钥,以确保即使应用程序被杀,跟踪仍然有效。

  3. 您可以使用上述任一方法之一。我使用第一种方法结合区域监控来触发本地通知。

    您可以查看以下Apple文档,以获得更详尽的解释和示例:

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

答案 1 :(得分:3)

基于区域(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的详细信息。

答案 2 :(得分:0)

你需要做两件事:

  1. 使用CLLocationManager startMonitoringSignificantLocationChanges。那是代码方面的问题。您可能希望收听通知,并根据您获得的信息执行任何操作。但是,如果应用程序没有运行,除非您执行两项操作,否则您将无法获取此信息:
  2. 在功能中:启用BackgroundModes和模式:位置更新。这样,即使您的应用从非活动列表中删除,我相信您也会收到通知。
  3. 但是,用户仍然可以将其关闭(设置 - >常规 - >后台应用刷新)。

答案 3 :(得分:0)

当app在后台时你可以这样做。 首先..Call CLLocationManager会在应用程序转到后台时删除。 每当设备获得新的纬度和经度时..此删除将被解雇。 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations; 现在调用Web服务并更新您当前的位置。 然后发送推送通知。