【问题标题】:location based push notification when app is not running?应用程序未运行时基于位置的推送通知?
【发布时间】:2013-04-01 04:01:05
【问题描述】:

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

如果可能的话,您能否就如何实现它提供一些指导?

【问题讨论】:

标签: ios ios5 location apple-push-notifications


【解决方案1】:

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

  1. 重要地点变更服务。

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

    如果您选择此方法,您不需要UIBackgroundModes 中设置location 键。

  2. 标准定位服务。

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

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

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

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

【讨论】:

  • 标准位置服务事件不会重新启动应用程序,如果它被杀死。 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】:

你需要做两件事:

  1. 使用 CLLocationManager 启动MonitoringSignificantLocationChanges。那是在代码方面。您可能想收听通知,并使用您获得的信息做任何事情。但是,如果应用没有运行,除非您执行以下两项操作,否则您将无法获得此信息:
  2. 在功能中:启用背景模式和模式:位置更新。这样一来,我相信即使您的应用从非活动列表中滑落,您也会始终收到通知。

但是,用户仍然可以将其关闭(设置->常规->后台应用刷新)。

【讨论】:

    【解决方案4】:

    您可以在应用程序处于后台时执行此操作。 首先..每当应用程序进入后台时调用 CLLocationManager deleagte。 每当设备将获得新的纬度和经度.. 这个委托将被解雇。 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations; 现在调用网络服务并更新您的当前位置。 然后发送推送通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多