Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook 3.0 : How to detect user cancel the permission dialog on android

I am having trouble to know how to detect that the user has cancelled the permission request dialog on Android.

Does anyone know what function do I need to call to check this?

like image 961
LittleFunny Avatar asked Dec 06 '25 03:12

LittleFunny


1 Answers

First things first: the question is a bit old and this answer assumes you are using a more recent version (I'm using 3.7 at the moment). It might be possible to do this using the 3.0 version of the SDK, but you should try and update the Facebook SDK that you are using in your project to a newer version as they have added quite a lot features and fixes since 3.0.

I'm using a Session.StatusCallback to listen for Session lifecycle changes, which I register to a UiLifecycleHelper that provides the updates for my activity.

The UiLifecycleHelper will fire an update when the user has finished interacting with the permission dialog, calling your Session.StatusCallback.call(Session, SessionState, Exception) method on your Session.StatusCallback instance it has registered. In order to detect the user cancelling the permission request, you can handle the Exception that you are being passed in to this method.

In my code, I'm doing it like this when posting, but read permission requests should be similar:

@Override
public void call(Session session, SessionState state,Exception exception) 
{
    if(exception != null)
    {
        if(exception instanceof FacebookOperationCanceledException)
        {
            //make sure we don't continue posting
            posting = false;
            //the user cancelled it, no need to show a message or do anything
            return;
        }
        else if(exception instanceof FacebookAuthorizationException)
        {
            Toast.makeText(FacebookActivity.this, "Failed to obtain Facebook permission to post on your behalf", Toast.LENGTH_LONG).show();
            //don't continue posting, let the user retry it if he/she wants to
            posting = false;
            return;
        }
    }

    //continue with checking that all permissions have been granted and post the action
}

I've found the info in the answer to this question, but I'm handling the FacebookAuthorizationException differently.

Also, if you restore the Session when posting or using another extended permission, I've found useful to check the result given by the SDK, as the user might have cancelled the permission from somewhere else, like the Facebook website. When the latter happens, you Session object in the app will indicate that it still holds the permission (such as publish_actions), while in reality it doesn't have that permission anymore. This happens when the Session object is cached and the data in it is not fresh.

This is what I'm doing to check that the permission is not missing (i.e. it has been removed from somewhere else, not from the app):

//note:
//I'm returning null further down the line because
//this code is run in an AsyncTask<Void,Void,Void>

Request request = new Request(
    session,
    graphPath,
    params,
    HttpMethod.POST);
Response response = request.executeAndWait();
GraphObject responseObject = response.getGraphObject();

if(responseObject == null)
{
    FacebookRequestError fbre = response.getError();
    if((fbre!=null) && (fbre.getCategory() == Category.PERMISSION))
    {
        requestFacebookPermissions(session);
        return null;
    }
    else
    {
        postFailed();
        return null;
    }
}
like image 103
lucian.pantelimon Avatar answered Dec 08 '25 20:12

lucian.pantelimon