2015-02-10 54 views
3

我正在使用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正在工作,因为我有很频繁调用的委托方法。

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

我想第二天解决问题,并找不到好的解决方案当用户出现在地方或离开地方时如何显示本地通知。我不知道如果使用UILocalNotificationregion属性对此解决方案有好处,或者我应该创建一种机制,它将搜索我已保存在应用程序中的位置并检查每次位置更改。对这个主题的任何意见也赞赏;)

谢谢你提前。

+2

你解决了吗?我有同样的问题。谢谢! – Leandro 2015-03-07 01:36:36

+0

不幸的不是。我创建了自己的机制,它正在与CLLocationManager一起工作,并检查是否访问过地点,然后触发适当的通知。像魅力一样工作 – 2015-03-07 13:35:36

+0

对你有好处!我希望苹果解决这个问题。 – Leandro 2015-03-08 23:16:51

回答

1

我看,你是不是在注册使用本通知

设设置= UIUserNotificationSettings(forTypes:notificationType,类别:类别) application.registerUserNotificationSettings(设置)

你可以在这里找到更多的信息iOS 8 Notifications in Swift和这里CoreLocation and region Monitoring

+0

我的不好。没有在片段中添加这个,但我做到了。正如你可以阅读本地通知没有'CLRegion'正在工作。编辑的初始文章。 – 2015-02-11 20:07:54

0

我有同样的问题,但我找到了一种方法来触发通知。 看起来它不响应radius参数。我触发通知的方式是模拟远离我所在地区的位置。

因此,如果我为丹麦的一个小城市设置区域,并将我的位置移动1公里并返回,则不会触发。 但是,如果我将该地区设置为丹麦的同一个小城市,并将我的位置移动到伦敦并返回,则会触发然后

对我来说,它看起来像半径参数是不知所何disgarded?我在半径250.0,100.0,10.0,0.0001上试过,它根本没有任何影响。

这可能会启发别人来解决问题,以及如何解决它?