1

我无法使用 Nearby.Messages 发布消息,尽管订阅方法似乎运行良好。我已经在我的 Google Developer Console 中为 Nearby 创建了新凭据,确保我的计算机的 SHA-1 和 PlayStore 中的应用程序都与这些凭据以及应用程序的包名称相关联。

当我在 onStart 中为 Publish 和 Subscribe 添加了 OnSuccessListener 时,我注意到 Subscribe 方法运行良好,而 Publish 方法失败。错误代码是 2806 - 禁止。

它曾经按原样工作。只有在我将应用程序上传到 Play 商店并且必须添加新的 OAuth 2.0 客户端 ID 后,它才停止工作,发布所需的消息。我也保留了以前的 ID,因此我继续在我的设备上进行测试,从 Android Studio 安装。同时,我还在控制台中重新创建了两个 ID,但问题仍然存在。在控制台上,附近功能也处于活动状态。

在下面添加片段的代码:

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();
    }

}

非常感谢您的帮助!此致,

4

0 回答 0