I've got the Nearby API set up in my Swift app and I can receive messages when the app is in the foreground. Following the instructions in the docs
I try to include params.allowInBackground = true
in the appropriate place but I get an error:
Value of type 'GNSBeaconStrategyParams' has no member 'allowInBackground'
So, I can't do that and my GNSSubscription object looks like this:
subscription = messageManager.subscriptionWithMessageFoundHandler(
messageFoundHandler, messageLostHandler: messageLostHandler,
paramsBlock: { (params: GNSSubscriptionParams!) in
params.deviceTypesToDiscover = .BLEBeacon
params.permissionRequestHandler = { (permissionHandler: GNSPermissionHandler!) in
// TODO: Show custom dialog here, and call permissionHandler after it is dismissed
// show the dialogue
}
params.beaconStrategy = GNSBeaconStrategy(paramsBlock: { (params: GNSBeaconStrategyParams!) in
params.includeIBeacons = true
//params.allowInBackground = true //*** THIS DOESN'T WORK ***
})
})
My messageHandlers look like this:
messageFoundHandler = {[unowned self](message: GNSMessage!) -> Void in
print("Found handler:", message.type, "->", String(data: message.content!, encoding:NSUTF8StringEncoding)!)
if UIApplication.sharedApplication().applicationState != .Active {
let localNotification = UILocalNotification()
localNotification.alertBody = "Message received" + String(data: message.content!, encoding:NSUTF8StringEncoding)!
UIApplication.sharedApplication().presentLocalNotificationNow(localNotification)
}
}
messageLostHandler = {(message: GNSMessage!) -> Void in
print("Lost Handler:", message.type, "->", String(data: message.content, encoding:NSUTF8StringEncoding)!)
}
With this set up and an Eddystone beacon in range I now get notifications in the background! Since this is what I want I should be happy. However, if I leave the device connected to xcode with the app in the background I start to see a stream of messages like this (seems to be roughly every 5 seconds):
2016-07-23 19:35:08.243 Hoc[1269:622746] Can't endBackgroundTask: no background task exists with identifier 2f3, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
I'm using v0.10.0 of NearbyMessages. If anyone can point me in the right direction to get background scanning working reliably on iOS that would be great.