Apple has really simplified how you can receive location based notifications in iOS 10, however, I'm finding that when the notification gets triggered and the UNUserNotificationCenterDelegate
delegate methods get called, the region objects that come down have the major and minor values always set to null. So when the delegate method for receiving the notification when the app is in the foreground, this method gets called:
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])
}
The CLBeaconRegion
object's major and minor NSNumber
variables are always null. I tell the CLLocationManager
to range for beacons, which should provide these values, shouldn't it? Any idea what's missing here? Or is this by design? I get the same result regardless of whether it's an actual beacon (e.g. KST particle) or another iOS device broadcasting over bluetooth using a CBPeripheralManager
.
Here's my notification registration code:
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)
}
didRangeBeacon
the major and minor. – Paulw11