我正在尝试在用户进入特定位置时向他们发送本地通知
I'm trying to send a local notification to the user when they enter a specific location
我不确定如何在特定位置触发此本地通知,有人可以帮忙吗?
请查找以下代码以获取更多信息。
func notification() {
let notification = UILocalNotification()
notification.alertBody = "Turn phone on silent"
notification.timeZone = NSTimeZone.localTimeZone()
notification.fireDate = NSDate().dateByAddingTimeInterval(5)
notification.repeatInterval = .Day
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print("Notification was sent")
}
要进行基于位置的通知,您应该在 CLLocation 管理器和 startMonitoringForRegion 的帮助下使用地理围栏,导入 CoreLocation 并重写通知方法如下:
func setNotificationForLatitude(latitude:CLLocationDegrees, longitude:CLLocationDegrees, identifier: String) {
var locationManager = CLLocationManager()
let regionToBeMointored = CLCircularRegion(center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), radius: 100.0, identifier: identifier)
//Configure Region depending on required latitude,longitude.
regionToBeMointored.notifyOnExit = false
//Configure event to be triggered on entry and exit
locationManager.startMonitoringForRegion(regionToBeMointored)
}
在 AppDelegate 中实现 locationManagerdidEnterRegion 委托方法并在此方法中配置本地推送通知:
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion)
{
let localGeoNotification:UILocalNotification = UILocalNotification()
localGeoNotification.alertBody = "You have entered the region"
localGeoNotification.fireDate = NSDate(timeIntervalSinceNow: 0)
localGeoNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(localGeoNotification)
locationManager.stopMonitoringForRegion(region)
}
我不确定如何在特定位置触发此本地通知,有人可以帮忙吗?
请查找以下代码以获取更多信息。
func notification() {
let notification = UILocalNotification()
notification.alertBody = "Turn phone on silent"
notification.timeZone = NSTimeZone.localTimeZone()
notification.fireDate = NSDate().dateByAddingTimeInterval(5)
notification.repeatInterval = .Day
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print("Notification was sent")
}
要进行基于位置的通知,您应该在 CLLocation 管理器和 startMonitoringForRegion 的帮助下使用地理围栏,导入 CoreLocation 并重写通知方法如下:
func setNotificationForLatitude(latitude:CLLocationDegrees, longitude:CLLocationDegrees, identifier: String) {
var locationManager = CLLocationManager()
let regionToBeMointored = CLCircularRegion(center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), radius: 100.0, identifier: identifier)
//Configure Region depending on required latitude,longitude.
regionToBeMointored.notifyOnExit = false
//Configure event to be triggered on entry and exit
locationManager.startMonitoringForRegion(regionToBeMointored)
}
在 AppDelegate 中实现 locationManagerdidEnterRegion 委托方法并在此方法中配置本地推送通知:
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion)
{
let localGeoNotification:UILocalNotification = UILocalNotification()
localGeoNotification.alertBody = "You have entered the region"
localGeoNotification.fireDate = NSDate(timeIntervalSinceNow: 0)
localGeoNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(localGeoNotification)
locationManager.stopMonitoringForRegion(region)
}