关于iOS后台扫描的Google Nearby API

时间:2016-07-23 19:01:27

标签: ios swift google-nearby

我已经在我的Swift应用程序中设置了Nearby API,当应用程序位于前台时,我可以收到消息。在instructions in the docs 之后我尝试将params.allowInBackground = true包含在适当的位置,但是我收到了错误消息:

Value of type 'GNSBeaconStrategyParams' has no member 'allowInBackground'

所以,我不能这样做,我的GNSSubscription对象看起来像这样:

    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 ***
                })
    })

我的messageHandlers看起来像这样:

    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)!)
    }

通过此设置和范围内的Eddystone信标,我现在可以在后台收到通知!既然这就是我想要的,我应该感到高兴。但是,如果我在背景中将设备与应用程序连接到xcode,我会开始看到这样的消息流(似乎大约每5秒钟):

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.

我正在使用NearbyMessages的v0.10.0。如果有人能够指出我正确的方向让背景扫描在iOS上可靠地工作,那就太好了。

1 个答案:

答案 0 :(得分:2)

这是由Cocoapods的问题引起的。即使我使用Tables和类似方法从我的项目中删除/重新添加NearbyMessages cocoaPod,由于某种原因我无法安装附近SDK的最新版本(1.0.1)。解决方案是从Github手动下载规范并覆盖$ pod update中的文件。

然后为了确保我安装了最新版本,我将podfile更改为包含~/.cocoapods/repos/master

现在我可以在GNSBeaconStrategy对象上设置适当的参数,并且在iOS上的后台运行Eddystone信标的背景扫描。 : - )

我希望这会有所帮助。