通知仅限于某个地理区域

时间:2018-02-13 07:50:52

标签: ios notifications gps

这是我刚刚开始思考的问题。 但有没有办法向给定iOS应用的用户发送通知,只有当他们碰巧在某个地理区域? 假设我是应用程序的所有者,并且可以修改代码以使其成为可能(如果可能的话)

更准确地说,应用程序具有某种发送通知的中央服务,但这些通知仅对给定区域中的人员感兴趣。我不想用那些不相关的通知来打扰那个区域以外的人。

2 个答案:

答案 0 :(得分:1)

适用于iOS 10及更高版本

如果您想使用UNNotificationRequest发送本地通知,可以执行以下操作:

  1. 要求requestAlwaysAuthorization并在NSLocationAlwaysUsageDescription文件中添加info.plist。还要将背景模式添加到位置。

    var locationManager:CLLocationManager
    

    viewWillAppear内部要求获得AlwaysAuthorization权限

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    
  2. 然后,使用startMonitoringSignificantLocationChangesstartUpdatingLocation来监控位置变化。

  3. 实施CLLocationManagerDelegate locationManager:didEnterRegion:并在进入特定位置时显示本地通知。

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion){
        //Show local notification when entered a desired location
        showLocalNotification("Entered \(region.identifier)")
    }
    
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion){
        //Show local notification when exit from a desired location
         showLocalNotification("Exited \(region.identifier)")
    }
    
  4. 对于早期版本

    如果您想使用UILocalNotification,可以在UILocalNotification对象中添加CLRegion。当用户进入/退出地理区域CLRegion时,iOS将自动显示本地通知。

    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)
    

    Here是来自Apple的链接,关于获取用户位置

答案 1 :(得分:0)

如果您使用本地通知,则可以使用地理围栏。有关详情,请查看this

在地理围栏中,您可以通知入口和退出通知。根据触发器,您可以设置通知。

相关问题