0

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.

James
  • 1,292
  • 1
  • 14
  • 29

1 Answers1

2

This was caused by an issue with Cocoapods. Even though I used $ pod update and similar approaches to remove/re-added the NearbyMessages cocoaPod from my project, for some reason I couldn't get the latest version (1.0.1) of the Nearby SDK installed. The solution was to manually download the spec from Github and overwrite the files in ~/.cocoapods/repos/master.

Then to make sure I had the latest version installed, I changed my podfile to include pod 'NearbyMessages', '~> 1.0.1' which worked.

Now I can set the appropriate params on the GNSBeaconStrategy object and background scanning for Eddystone beacons works in the background on iOS. :-)

I hope this helps.

James
  • 1,292
  • 1
  • 14
  • 29
  • James, what did you originally have in your Podfile? If you have `pod 'NearbyMessages', '~> 0.10.0'`, you wouldn't get the latest CocoaPod with background support. Just be aware that by using `pod 'NearbyMessages', '~> 1.0.1'`, you'll get bug fix updates to the 1.0 version, but you won't get version 1.1 when it comes out. – Dan Webb Jul 25 '16 at 20:44
  • Thanks @DanWebb I originally only had `pod 'NearbyMessages'` with no version number. I'll change it to get the updates but something weird is going on with my cocoapod/git setup so I might not get them anyway. Thanks – James Jul 25 '16 at 20:49