【发布时间】:2017-02-03 23:17:30
【问题描述】:
Apple 确实简化了您在 iOS 10 中接收基于位置的通知的方式,但是,我发现当通知被触发并调用 UNUserNotificationCenterDelegate 委托方法时,出现的区域对象具有主要和次要值始终设置为 null。所以当应用在前台接收通知的委托方法时,会调用这个方法:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// When the app is in the foreground
if let trigger = notification.request.trigger as? UNLocationNotificationTrigger,
let region = trigger.region as? CLBeaconRegion {
// When you examine the region variable here, its major and minor
// values are null
}
completionHandler([.alert, .sound])
}
CLBeaconRegion 对象的主要和次要 NSNumber 变量始终为空。我告诉CLLocationManager 设置信标的范围,它应该提供这些值,不是吗?知道这里缺少什么吗?或者这是设计使然?无论是实际的信标(例如 KST 粒子)还是其他使用CBPeripheralManager 通过蓝牙广播的 iOS 设备,我都会得到相同的结果。
这是我的通知注册码:
let locationManager = CLLocationManager()
func createLocationNotification() {
self.locationManager.requestWhenInUseAuthorization()
let region = CLBeaconRegion(proximityUUID: UUID(uuidString: "UUID-STRING")!, identifier: "com.company.identifier")
region.notifyOnEntry = true
region.notifyOnExit = false
let content = UNMutableNotificationContent()
content.title = "New Checkin Received"
content.body = "Swipe or tap this message to see who's here"
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
let request = UNNotificationRequest.init(identifier: "BeaconNotificationIdentifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
})
self.locationManager.startRangingBeacons(in: region)
}
【问题讨论】:
-
我认为传递给代理的 CLBeaconRegion 与您使用 UNLocationNotificationTrigger 注册的区域相同。由于这没有主要或次要,因此您在委托中没有主要或次要。您应该在通知委托方法中开始测距信标,然后从
didRangeBeacon主要和次要开始。
标签: ios swift ibeacon core-bluetooth