我正在开发简单的 To-Do 应用程序,它在 UILocalNotification
中使用 CLRegion
支持的 IOS 8 功能。
这是我用来安排本地通知的代码:
var schedule = false
let notification = UILocalNotification()
/// Schedlue with date
if let reminder = self.dateReminderInfo {
schedule = true
notification.fireDate = reminder.fireDate
notification.repeatInterval = reminder.repeatInterval
notification.alertBody = self.title
}
/// Schedule with location
if let reminder = self.locationReminderInfo {
schedule = true
let region = CLCircularRegion(circularRegionWithCenter: reminder.place.coordinate, radius: CLLocationDistance(reminder.distance), identifier: taskObjectID)
region.notifyOnEntry = reminder.onArrive
region.notifyOnExit = reminder.onArrive
notification.region = region
notification.regionTriggersOnce = false
}
/// Schedule
if schedule {
notification.userInfo = ["objectID": taskObjectID]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
用日期安排工作。我安排通知,退出应用程序,并在适当的时间在屏幕上显示通知,太棒了。问题是当我安排位置通知时。我以米为单位传递坐标和半径(例如 100 米)。该应用程序未显示任何内容。我在家里测试它,将点放在 1 公里的距离内并到达那里。没有显示通知。我也在玩模拟器,并将位置从可用位置更改为我附近的自定义位置并返回。没有显示通知。问题出在哪里?
在 AppDelegate 中,我正在注册通知:
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
这是定位服务的代码。
func startLocationService() {
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
let status = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.Denied {
let title = (status == CLAuthorizationStatus.Denied) ? "Location services are off" : "Background location is not enabled"
let message = "To use background location you must turn on 'Always' in the Location Services Settings"
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
/// Settings
alertController.addAction(UIAlertAction.normalAction("Settings", handler: { _ in
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
return
}))
/// Cancel
alertController.addAction(UIAlertAction.cancelAction(String.cancelString(), handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
} else if status == CLAuthorizationStatus.NotDetermined {
/// nothing
}
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
CLLocationManager 正在工作,因为我有经常调用的委托(delegate)方法。
/// Mark: CLLocationManagerDelegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
self.navigationItem.title = "\(location.coordinate.latitude)" + " " + "\(location.coordinate.longitude)"
println(self.navigationItem.title)
}
}
我正在尝试第二天解决问题,但找不到好的解决方案如何在用户出现或离开时显示本地通知。我不知道使用 UILocalNotification
region
属性是否适合此解决方案,或者我应该创建一种机制来搜索我在应用程序中保存的位置并每次位置更改时检查。对此主题的任何评论也表示赞赏;)
提前谢谢你。
最佳答案
我看到你没有注册通知
let settings = UIUserNotificationSettings(forTypes: notificationType, categories: 类别) application.registerUserNotificationSettings(设置)
您可以在此处找到更多信息 iOS 8 Notifications in Swift在这里 CoreLocation and region Monitoring
关于ios - 点击区域时不显示本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440949/