我在我的 Swift 应用程序中设置了 Nearby API,当应用程序在前台时我可以接收消息。按照文档中的说明,
我尝试将其包含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 上可靠地工作,那就太好了。