1

I am trying to publish a simple text message using Nearby API but it is returning unknown status code = 2806.Here is my sample code

 private void publish(String message) {
        Log.i(TAG, "Publishing message: " + message);
        mActiveMessage = new Message(message.getBytes());

        if (!mGoogleApiClient.isConnected()) {
            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
        } else {
            PublishOptions options = new PublishOptions.Builder()
                    .setStrategy(Strategy.DEFAULT)
                    .setCallback(new PublishCallback() {
                        @Override
                        public void onExpired() {
                            super.onExpired();
                            Log.i(TAG, "no longer publishing");
                        }
                    }).build();

            Nearby.Messages.publish(mGoogleApiClient, mActiveMessage,options)
                    .setResultCallback(new ResultCallback<Status>() {

                        @Override
                        public void onResult(Status status) {
                            if (status.isSuccess()) {
                                Log.i(TAG, "published successfully");
                            } else {
                                Log.i(TAG, "could not publish");
                            }
                        }
                    });
        }
    }

Do I need to add some special permission or anything else?

  • We're still having this error over here if anyone has any insight: https://stackoverflow.com/questions/64431212/google-nearby-messages-publish-returns-2806-forbidden-android-react-native – Perniferous Nov 03 '20 at 19:28

4 Answers4

6

2806=FORBIDDEN

Did you have the api key for your app specify in the manifest and ensure that it has the right packagename and sha1 fingerprint?

Tony Tran
  • 234
  • 1
  • 5
2

Make your API key restricted to your package name and SHA1 fingerprint.

  1. Go to Google API Console
  2. Go to Credentials
  3. If you have no API key yet, click Create Credentials and select API key. Else, select your current API key.
  4. Under key restriction, select Android apps.
  5. Enter the package name of your android app and SHA1 fingerprint.
  6. To find your SHA1 fingerprint:
    • open command prompt
    • type your current Java JDK bin path (e.g. cd C:\Program Files\Java\jdk1.8.0_77\bin)
    • type keytool -list -v -keystore "PATH_OF_YOUR_KEYSTORE_FILE" (Note: The debug.keystore may be found in .android folder (e.g. C:\Users\myUser\.android\debug.keystore)
    • password is android
    • copy the SHA1 fingerprint
  7. Click Save

Ensure that you have the google Nearby dependency in your build.gradle.

compile 'com.google.android.gms:play-services-nearby:10.0.1'

Make sure to also enable the Nearby Messages API in the Google Console.

  1. Go to Google API Console.
  2. Go to Library.
  3. Search for Nearby Messages API.
  4. Click the Enable button beside the Nearby Messages API title.
1

This error can also occur if you only subscribe/publish with an non-Activity Context (e.g. Application or Service). The library needs an Activity to show the opt-in message. Try to subscribe within an Activity (as well as from a Service).

crysxd
  • 3,177
  • 20
  • 32
0

Did you check if the Google Play Services installed on your devices are up to date ? Or at least do they the match the version you are using in your build.gradle ?

almighty972
  • 1,158
  • 2
  • 6
  • 5