José
José

Reputation: 3164

Google's nearby messages: not receiving any messages

I am trying out Google's nearby messages API, which seems to be easy to use, but it's for some reason not working as expected. I suspect that the problem is something trivial, but I have not been able to solve this.

I double-checked that the API-key is correct and I have also added permissions for NSMicrophoneUsageDescription and NSBluetoothPeripheralUsageDescription in the Info.plist.

The Nearby Messages API is enabled in Google's developer console and the API keys has been set to be restricted to the app's bundle identifier. It won't work either if this restrictions is removed.

class ViewController: UIViewController {

    private var messageManager: GNSMessageManager?

    override func viewDidLoad() {
        super.viewDidLoad()

        GNSMessageManager.setDebugLoggingEnabled(true)

        messageManager = GNSMessageManager(apiKey: "<my-api-key>", paramsBlock: { (params: GNSMessageManagerParams?) -> Void in
            guard let params = params else { return }

            params.microphonePermissionErrorHandler = { hasError in
                if hasError {
                    print("Nearby works better if microphone use is allowed")
                }
            }
            params.bluetoothPermissionErrorHandler = { hasError in
                if hasError {
                    print("Nearby works better if Bluetooth use is allowed")
                }
            }
            params.bluetoothPowerErrorHandler = { hasError in
                if hasError {
                    print("Nearby works better if Bluetooth is turned on")
                }
            }
        })

        // publish
        messageManager?.publication(with: GNSMessage(content: "Hello".data(using: .utf8)))

        // subscribe
        messageManager?.subscription(messageFoundHandler: { message in
            print("message received: \(String(describing: message))")
        }, messageLostHandler: { message in
            print("message lost: \(String(describing: message))")
        })
    }

}

Did anybody else have issues setting this up?

Upvotes: 4

Views: 279

Answers (1)

Jos&#233;
Jos&#233;

Reputation: 3164

Ok, for whoever has the same problem, the solution was quite simple and almost embarrassing. It is necessary to hold the publication and the subscription result in a class variable:

    private var publication: GNSPublication?
    private var subscription: GNSSubscription?

    override func viewDidLoad() {
        super.viewDidLoad()

        messageManager = GNSMessageManager(apiKey: "<my-api-key>")

        // publish
        publication = messageManager?.publication(with: GNSMessage(content: "Hello".data(using: .utf8)))

        // subscribe
        subscription = messageManager?.subscription(messageFoundHandler: { message in
            print("message received: \(String(describing: message))")
        }, messageLostHandler: { message in
            print("message lost: \(String(describing: message))")
        })
    }

Upvotes: 2

Related Questions