2017-02-03 21 views
1

Apple已经真正简化了如何在iOS 10中接收基于位置的通知,但是,我发现当通知被触发并调用UNUserNotificationCenterDelegate委托方法时,下来的区域对象的主值和次值始终设置为空。所以,当接收时,应用程序是在前台通知委托方法,该方法被调用:iOS 10灯塔基于位置的通知不提供主要和次要值

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

我想像一下,传递给委托的CLBeaconRegion与您在UNLocationNotificationTrigger中注册的区域相同。由于这没有大的或未成年人,你在代表中没有主修或未成年人。你应该在通知委托方法中启动测距信标,然后从'didRangeBeacon'中找出主要和次要信号。 – Paulw11

回答

1

UNLocationNotificationTrigger是围绕灯塔监控API一个便利的包装,没有信标范围的API。 监控API根本不报告检测到的单个标识符,只有CLBeaconRegion用作模式过滤器来设置监控。您无法使用监控来确定检测到的确切标识符。

如果你看看API的工作原理,这是有道理的。 UNLocationNotificationTrigger有一个区域属性是CLRegion。它没有CLBeacon属性,为了检测单个标识符,必须使用它。尽管可能有CLBeaconRegion的标识符已完全填充,但如果iOS按照您的喜好行事,则必须构建新的CLRegion实例,以便使用检测到的特定信标标识符来填充其字段。

不幸的是,作为@ Paulw11在他的评论中建议的替代方案是不使用此便利包装,并使用信标来手动触发您的通知。

+0

好的。那么,我想我不会再追逐那条尾巴。感谢指针。这看起来太好了,但是事实是'UNLocationNotificationTrigger'接受了'CLBeaconRegion'让我充满希望。好吧。再次感谢。 –