【问题标题】:iOS 10 Beacon Location Based Notifications Not Providing Major and Minor ValuesiOS 10 信标基于位置的通知不提供主要和次要值
【发布时间】: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


【解决方案1】:

UNLocationNotificationTrigger 是信标监控 API 的便捷包装,而不是信标测距 API。 监控 API 根本不报告检测到的单个标识符,只报告 CLBeaconRegion 用作模式过滤器来设置监控。您不能使用监控来确定检测到的确切标识符。

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

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

【讨论】:

  • 好的。好吧,我想我不会再追我的尾巴了。感谢您的指点。这似乎好得令人难以置信,但UNLocationNotificationTrigger 接受CLBeaconRegion 的事实让我充满希望。那好吧。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 2015-02-27
  • 2019-03-14
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多