简体   繁体   English

Android“使用Facebook登录”回调未触发

[英]Android “Login With Facebook” callback not getting fired

I'm integrating "Login With Facebook" into my Android app and I'm facing a weird issue that is the first time ( and only the first ) that I launch my app (just after a fresh new copy has been installed) Facebook callback is not being called after I hit the button (but does the login normally). 我正在将“使用Facebook登录”集成到我的Android应用程序中,并且遇到了一个奇怪的问题,这是我第一次( 也是第一次 )启动我的应用程序(安装了新的新副本之后)Facebook回调按下按钮后不会被调用(但正常登录)。 If I login normally (or with Facebook) and then do a logout second time and onwards the callback gets fired normally. 如果我正常登录(或使用Facebook),然后再次注销,则回调将正常触发。

I need the callback to do some user session operations so I'm getting mad about this, many days without being able to find a solution so I hope anyone can help me. 我需要回调来执行一些用户会话操作,所以我为此感到生气,很多天都找不到解决方案,所以希望有人能帮助我。 My facebookCallBackManager() function is called in onCreate. 我的facebookCallBackManager()函数在onCreate中调用。

In my example below I need getFacebookUserData() method to get fired but setting breakpoints and debugging not even onSucces is getting fired. 在下面的示例中,我需要使用getFacebookUserData()方法来触发,但是设置断点和调试甚至不会触发onSucces。

Thanks in advance! 提前致谢!

Below I paste my callback code in my ActivityLogin class: 下面,我将回调代码粘贴到ActivityLogin类中:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

private void facebookCallBackManager() {

    callbackManager = CallbackManager.Factory.create();

    LoginButton btnFacebookLogin = (LoginButton) findViewById(R.id.btnFacebookLogin);
    btnFacebookLogin.setReadPermissions("public_profile", "email", "user_friends");
    btnFacebookLogin.registerCallback(callbackManager,
        new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                GraphRequest.newMeRequest(
                    loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject json, GraphResponse response) {
                            if (response.getError() != null) {
                                // handle error
                            } else {
                                getFacebookUserData(json);
                            }
                        }
                    }
                ).executeAsync();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException exception) {

                if (!GeneralUtils.isOnline(oContext)){
                    GeneralUtils.showNoInternet(strTheme, oContext, ActivityLogin.this);
                }else{
                    String strMessage=exception.getMessage();
                    GeneralUtils.showToast(strMessage, Toast.LENGTH_LONG, strTheme, ActivityLogin.this);
                }
            }
        });
}

Edit 1: 编辑1:

I forgot to mention I'm using SDK 4.23.0, so SDK initialization is no longer needed. 我忘了提到我正在使用SDK 4.23.0,因此不再需要SDK初始化。

Edit 2: 编辑2:

More information that might be useful. 更多有用的信息。

My ActivityMain is the main and default one and when launching if user is not logged in then redirection to login is done with the next lines: 我的ActivityMain是主要的和默认的ActivityMain,在启动时,如果用户未登录,则使用以下几行完成登录的重定向:

private void doLogin(){

    Intent i = new Intent(getApplicationContext(), ActivityLogin.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

    this.startActivity(i);
}

Well, I will answer my own question just in case it helps anyone who might be facing the same (or similar) problem, but first I have to say @Michal_196 have put me in the right direction so I first say thanks to him. 好吧,我会回答我自己的问题,以防万一它可以帮助可能遇到相同(或相似)问题的任何人,但是首先我必须说@ Michal_196使我朝着正确的方向前进,因此我首先感谢他。

The problem of the callback not being fired the first time the app was launched has to do with the way I was calling ActivityLogin activity, in concrete: setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 具体来说,setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);第一次启动该应用程序时不触发回调的问题与我调用ActivityLogin活动的方式有关。

My flow is to call ActivityMain and check for logged user and in case it is not I redirect to ActivityLogin and to avoid than in login after back button a user who was not logged was able to hit back and just access the app I've had added "FLAG_ACTIVITY_NO_HISTORY" to clear main from history but I don't know why that was the culprit of callback not getting fired. 我的流程是调用ActivityMain并检查已登录的用户,如果不是,则我将重定向到ActivityLogin,并避免在后退按钮后登录,而未登录的用户可以进行回退并只访问我拥有的应用程序添加了“ FLAG_ACTIVITY_NO_HISTORY”以清除历史记录中的主要内容,但我不知道为什么这是未触发回调的罪魁祸首。

After removing that line not only callback works correctly but also if a user starts the app and is redirected to login from main and hits back button without logging app just exits as expected, weird but works! 删除该行之后,不仅回调可以正常工作,而且如果用户启动该应用程序并被重定向到主登录名并单击后退按钮而不登录该应用程序,则该行仅按预期退出,这很奇怪,但是可以正常工作!

Edit 1: 编辑1:

Thinking better now I understand what has happened, FLAG_ACTIVITY_NO_HISTORY instead of clearing main activity from stack was indicating that login activity shouldn't be put on stack and so callback was not getting fired after facebook was called, it's clear to me now and I've solved my problem. 现在想得更好,我了解发生了什么,FLAG_ACTIVITY_NO_HISTORY不是从堆栈中清除主要活动,而是指示不应将登录活动放在堆栈中,因此在调用facebook之后回调未触发,这对我来说很明显,我已经解决了我的问题。 Thanks again @Michal_196. 再次感谢@ Michal_196。

findViewById(R.id.btn_facebook_login).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { findViewById(R.id.btn_facebook_login).setOnClickListener(new View.OnClickListener(){@Override public void onClick(View v){

            LoginManager.getInstance().logInWithReadPermissions(LoginScreen.this, Arrays.asList("public_profile", "email", "user_friends"));
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            // TODO Auto-generated method stub

                            try {
                                String fb_id = "";
                                String email_Id = "";
                                String name = "";
                                String gender = "";
                                if (response.getJSONObject().has("id")) {
                                    fb_id = response.getJSONObject().getString("id");
                                }
                                email_Id = response.getJSONObject().getString("email");
                                name = response.getJSONObject().getString("name");
                                gender = response.getJSONObject().getString("gender");
                                String url = "https://graph.facebook.com/" + fb_id + "/picture?type=large";   //small , large

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,email,gender, birthday");
                    request.setParameters(parameters);
                    request.executeAsync();
                }

                @Override
                public void onCancel() {
                    Log.d("TAG", "FB_LoginResponse==> Cancel");
                }

                @Override
                public void onError(FacebookException exception) {
                    Log.d("TAG", "Exception_FB_LoginResponse==>" + exception.getMessage());
                }
            });
        }
    });

======================================================================== add this in your oncreate method ================================================== ======================将其添加到您的oncreate方法中

FacebookSdk.sdkInitialize(this); FacebookSdk.sdkInitialize(本);

FacebookSdk.setApplicationId("here is you facebook application id"); FacebookSdk.setApplicationId(“这是您的Facebook应用程序ID”);

callbackManager = CallbackManager.Factory.create(); callbackManager = CallbackManager.Factory.create();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM