Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using share dialog in Android Facebook SDK. How to know is user actually shared or cancelled sharing activity?

I have added sharing functionality to Android app as described here https://developers.facebook.com/docs/android/share-dialog/#setup

But I have noticed that if user is cancelled sharing activity onComplete is called anyway

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

    uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
        @Override
        public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
            Log.e("Activity", String.format("Error: %s", error.toString()));
        }

        @Override
        public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
            Log.e("Activity", "Success!");
        }
    });
}

I have also looked in to Bundle which is returned. Even if I cancel share dialog I get

com.facebook.platform.extra.DID_COMPLETE=true

How can I get result that user really shared data on facebook? (Without making separate login with facebook button. Maybe some permissions need to be added?)

like image 284
Elizaveta Ivanova Avatar asked Nov 02 '13 19:11

Elizaveta Ivanova


3 Answers

See https://developers.facebook.com/docs/android/share-dialog/#handling-responses

You can tell if the user has cancelled by calling

String gesture = FacebookDialog.getNativeDialogCompletionGesture(data);
if (gesture != null) {
  if ("post".equals(gesture)) {
    // the user hit Post
  } else if ("cancel".equals(gesture)) {
    // the user hit cancel
  } else {
    // unknown value
  }
} else {
  // either an error occurred, or your app has never been authorized
}

where data is the result bundle. However, it will only return a non-null value IF the user has logged in via your app (i.e. you have at least basic_info permissions). If the user has never logged in or authorized your app, then the only thing you'll see is the DID_COMPLETE, and it will always be true unless an error occurred. This is by design.

like image 106
Ming Li Avatar answered Nov 07 '22 19:11

Ming Li


In order to obtain the result for the sharing, your app needs to have at least the basic_info permission.

To solve that, just open an session (this will automatically request the basic_info permission):

Session.openActiveSession(this /*your activity*/, 
                          true /*allows the UI login to show up if needed*/, 
                          new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        Log.i("[Facebook]", "Session: " + state.toString());
        if (session.isOpened()) {
           /// now you are good to get the sharing results
        }
    }
});

You can find more information in here: https://developers.facebook.com/docs/android/getting-started/

like image 43
JHNeves Avatar answered Nov 07 '22 19:11

JHNeves


Implement FacebookCallback<Sharer.Result> to know whether sharing was successful or cancelled or there was an error.

You can use the code below in Activity and in Fragment as well. When using in Fragment make sure you pass this in ShareDialog constructor. If you pass getActivity() then onActivityResult method will not be triggered in Fragment.

private CallbackManager callbackManager;

private void shareYourContentOnFacebook() {

    callbackManager = CallbackManager.Factory.create();
    ShareDialog shareDialog = new ShareDialog(this);
    shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Sharer.Result result) {
            Log.d(this.getClass().getSimpleName(), "shared successfully");
            //add your code to handle successful sharing
        }

        @Override
        public void onCancel() {
            Log.d(this.getClass().getSimpleName(), "sharing cancelled");
            //add your code to handle cancelled sharing

        }

        @Override
        public void onError(FacebookException error) {
            Log.d(this.getClass().getSimpleName(), "sharing error");
            //add your code to handle sharing error

        }
    });

    if (ShareDialog.canShow(ShareLinkContent.class)) {

        ShareLinkContent shareLinkContent = new ShareLinkContent.Builder()
                .setContentTitle("Your content title")
                .setContentDescription("Your content description")
                .setContentUrl(Uri.parse(""http://your-content-url.com""))
                .build();

        shareDialog.show(shareLinkContent);

    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}
like image 3
dzikovskyy Avatar answered Nov 07 '22 17:11

dzikovskyy