Android "Login With Facebook" callback not getting fired

时间:2017-06-14 10:26:18

标签: android facebook login callback

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). If I login normally (or with Facebook) and then do a logout second time and onwards the callback gets fired normally.

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.

In my example below I need getFacebookUserData() method to get fired but setting breakpoints and debugging not even onSucces is getting fired.

Thanks in advance!

Below I paste my callback code in my ActivityLogin class:

@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:

I forgot to mention I'm using SDK 4.23.0, so SDK initialization is no longer needed.

Edit 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:

private void doLogin(){

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

    this.startActivity(i);
}

2 个答案:

答案 0 :(得分:0)

好吧,我会回答我自己的问题,万一它可以帮助任何可能面临同样(或类似)问题的人,但首先我要说@ Michal_196让我朝着正确的方向前进,所以我首先要感谢他。

第一次启动应用程序时没有触发回调的问题与我调用ActivityLogin活动的方式有关,具体如下:setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

我的流程是调用ActivityMain并检查已记录的用户,如果不是我重定向到ActivityLogin并避免在登录后退回按钮,未登录的用户能够回击并只访问应用程序我我已经添加了“FLAG_ACTIVITY_NO_HISTORY”以清除历史记录中的主要内容,但我不知道为什么这是回调未被解雇的罪魁祸首。

删除该行后不仅回调正常工作,而且如果用户启动应用程序并被重定向到从主登录并点击后退按钮而没有记录应用程序只是按预期退出,很奇怪但是有效!

编辑1:

现在好好思考我理解发生了什么,FLAG_ACTIVITY_NO_HISTORY而不是清除堆栈中的主要活动表明登录活动不应该放在堆栈上,所以在调用facebook之后回调没有被解雇,现在我很清楚我已经解决了我的问题。再次感谢@ Michal_196。

答案 1 :(得分:0)

findViewById(R.id.btn_facebook_login).setOnClickListener(new View.OnClickListener(){             @覆盖             public void onClick(查看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());
                }
            });
        }
    });

=============================================== ========================= 在你的oncreate方法中添加它

FacebookSdk.sdkInitialize(本);

FacebookSdk.setApplicationId(“这是你的facebook应用程序ID”);

callbackManager = CallbackManager.Factory.create();