gpt4 book ai didi

ios - 通知仅限于一个地理区域

转载 作者:行者123 更新时间:2023-11-29 00:03:25 25 4
gpt4 key购买 nike

这是我才开始考虑的事情。但是有没有办法向给定 iOS 应用程序的用户发送通知,只有当他们碰巧在某个地理区域时?假设我是该应用程序的所有者,并且可以修改代码以实现这种可能性(以防万一)

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

最佳答案

适用于 iOS 10 及更高版本

如果你想用UNNotificationRequest发送本地通知,你可以按如下方式执行:

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

    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)")
    }

对于早期版本

如果你想使用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 的链接,关于获取用户的位置

关于ios - 通知仅限于一个地理区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48761880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com