iOS 10 个基于信标位置的通知未提供主要值和次要值

iOS 10 Beacon Location Based Notifications Not Providing Major and Minor Values

Apple 确实简化了在 iOS 10 中接收基于位置的通知的方式,但是,我发现当通知被触发并且 UNUserNotificationCenterDelegate 委托方法被调用时,该区域下来的对象的主要和次要值始终设置为空。所以当应用程序在前台接收通知的委托方法时,这个方法被调用:

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

UNLocationNotificationTrigger 是围绕信标监视 API 的便利包装器,而不是信标范围 APIs。 监控 APIs 根本不报告检测到的个人标识符,仅报告用作模式过滤器的 CLBeaconRegion 来设置监控。 您不能使用监控来找出检测到的确切标识符。

如果您查看 API 的工作原理,这就很有意义了。 UNLocationNotificationTrigger 有 属性 个区域,即 CLRegion。它没有 CLBeacon 属性,它必须有才能检测各个标识符。虽然可以有一个 CLBeaconRegion 的标识符已完全填充,但如果 iOS 的行为方式符合您的要求,则必须构建一个新的 CLRegion 实例以便用检测到的特定信标标识符填充其字段。

不幸的是,正如@Paulw11 在他的评论中建议的那样,另一种选择是不使用此便利包装器并使用信标测距来手动触发您的通知。