Nearby.Messages API seems not to be working

301 views Asked by At

I'm not being able to publish messages with Nearby.Messages, although the Subscribe method seems to be working well. I've already created new credentials for Nearby in my Google Developer Console, made sure both the SHA-1's of my computer, and the app in the PlayStore are associated with those credentials, as well as the app's package name.

As I added an OnSuccessListener to both Publish, and Subscribe, within onStart, I noticed that the Subscribe method works well, while the Publish one Fails. The error code is 2806 - Forbidden.

It used to work just the way it is. Only after I uploaded the app to the Play Store, and had to add a new OAuth 2.0 Client ID, did it stop working, on publishing the desired Message. I've maintained the previous ID, as well, so I continue testing on my device, installing from Android Studio. Meanwhile, I've also re-created both ID's in the Console, but the problem still occurs. The Nearby function is also active, on the Console.

Adding the Fragment's code below:

package co.thanker.fragments;

import android.graphics.PorterDuff;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.google.android.gms.nearby.Nearby;
import com.google.android.gms.nearby.messages.Message;
import com.google.android.gms.nearby.messages.MessageListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;

import co.thanker.R;

public class FindFragment extends Fragment {

    private final String TAG = "FindFragment";
    private final String OUR_USER_ID = "our-user-id";
    private final String OUR_USER_COUNTRY = "our-user-country";
    private final String USER_ID_STRING = "user-id-string";
    private final String THANKER_ID_STRING = "thanker-id-string";
    private final String USER_COUNTRY = "user-country";
    private final String CONTINUE_SENDING_ID = "continue-sending-user-id";
    private final String ACTIVATED_THANKS = "activated-thanks";

    private Message mSendingMessage;
    private MessageListener mMessageListener;

    private FirebaseAuth mAuth;

    private String mCountry;
    private String mUserId;
    private boolean mActivatedThanks;
    private ImageView mGifFind;
    private ProgressBar mProgressBar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_find, container, false);

        Log.v(TAG, "Entering FindFragment");

        mAuth = FirebaseAuth.getInstance();
        mActivatedThanks = true;
        mGifFind = (ImageView) view.findViewById(R.id.gif_find);
        mProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar);

        if(getArguments() != null){
            Log.v(TAG, "Finding passing Country. Thanks Fragment. GetArguments() exists");
            mUserId = getArguments().getString(THANKER_ID_STRING);
            mCountry = getArguments().getString(OUR_USER_COUNTRY);
        }

        else {
            Log.v(TAG, "Finding passing Country. Thanks Fragment. GetArguments() does not exist");
            mUserId = mAuth.getCurrentUser().getUid();
        }

        Log.v(TAG, "Nearby. Thanks got in Thanks Fragment: " + mCountry + ". User ID: " + mUserId);

        if(getActivity() != null){
            Glide.with(getActivity()).load(R.drawable.nearby_search).into(mGifFind);
            mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark), PorterDuff.Mode.SRC_IN );
        }

        mMessageListener = new MessageListener() {
            @Override
            public void onFound(Message message) {
                Log.d(TAG, "Nearby. Found message: " + new String(message.getContent()));

                final String otherUserId = new String(message.getContent()).trim();

                if(!otherUserId.equalsIgnoreCase(mAuth.getCurrentUser().getUid())){
                    Fragment otherUserProfileFragment = new OtherProfileFragment();
                    Bundle userInfoBundle = new Bundle();
                    userInfoBundle.putString(USER_ID_STRING, otherUserId);
                    userInfoBundle.putString(OUR_USER_ID, mUserId);
                    userInfoBundle.putString(OUR_USER_COUNTRY, mCountry);
                    userInfoBundle.putBoolean(ACTIVATED_THANKS, mActivatedThanks);
                    userInfoBundle.putBoolean(CONTINUE_SENDING_ID, true);

                    otherUserProfileFragment.setArguments(userInfoBundle);

                    if(getActivity() != null){
                        mProgressBar.setVisibility(View.GONE);
                        getActivity().getSupportFragmentManager().beginTransaction()
                                .replace(R.id.fragment_container, otherUserProfileFragment).addToBackStack(null).commit();
                    }
                }

             }

            @Override
            public void onLost(Message message) {
                Log.d(TAG, "Nearby. Lost sight of message: " + new String(message.getContent()));
            }
        };
        
        byte [] userIdInBytes = mUserId.getBytes();
        mSendingMessage = new Message(userIdInBytes);
        Log.v(TAG, "Nearby. Sending Message: " + new String(mSendingMessage.getContent()));

        return view;
    }

    @Override
    public void onStart(){
        super.onStart();

        if(getActivity() != null){

            Nearby.getMessagesClient(getActivity()).publish(mSendingMessage).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.v(TAG, "Nearby. Publishing key");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.v(TAG, "Nearby. Couldn\'t publish key. Error: " + e.toString());
                }
            });

            Nearby.getMessagesClient(getActivity()).subscribe(mMessageListener).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.v(TAG, "Nearby. Subscribing incoming message");
                }
            });

        }
    }

    @Override
    public void onStop(){

        if(getActivity() != null){
            Nearby.getMessagesClient(getActivity()).unpublish(mSendingMessage);
            Nearby.getMessagesClient(getActivity()).unsubscribe(mMessageListener);
            Log.v(TAG, "Nearby. Unpublished keys");
        }

        super.onStop();
    }

}

Really appreciate your help! Best regards,

0

There are 0 answers