【问题标题】:Local notification doesn't show up when hitting region点击区域时不显示本地通知
【发布时间】:2015-04-11 00:47:28
【问题描述】:

我正在开发简单的待办事项应用程序,该应用程序使用CLRegion 支持的 IOS 8 功能 UILocalNotification

这是我用来安排本地通知的代码:

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

我正在尝试第二天解决问题,但找不到好的解决方案如何在用户出现在该地点或离开该地点时显示本地通知。我不知道使用 UILocalNotification region 属性是否适合此解决方案,或者我应该创建一种机制来搜索我保存在应用程序中的位置并检查每次位置更改。对此主题的任何 cmet 也表示赞赏;)

提前谢谢你。

【问题讨论】:

  • 你解决了吗?我有同样的问题。谢谢!
  • 很遗憾没有。我创建了自己的机制,它与 CLLocationManager 一起工作并检查是否已访问过地点,然后触发适当的通知。像魅力一样工作
  • 对你有好处!我希望苹果能解决这个问题。

标签: ios swift core-location uilocalnotification clregion


【解决方案1】:

我发现您没有注册通知

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

你可以在这里iOS 8 Notifications in SwiftCoreLocation and region Monitoring找到更多信息

【讨论】:

  • 我的错。没有在 sn-ps 中添加这个,但我做到了。你可以在没有CLRegion 的情况下阅读本地通知。已编辑初始帖子。
【解决方案2】:

我遇到了同样的问题,但是我找到了触发通知的方法。 看起来它没有响应半径参数。我触发通知的方式是模拟离我所在区域很远的位置。

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

所以对我来说,半径参数似乎被忽略了?我在 250.0、100.0、10.0、0.0001 上尝试了半径,它完全没有影响。

这能否启发其他人了解问题可能是什么以及如何解决?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多