Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-native-fbsdk ShareDialog.show promise not resolves in Android #12457

Closed
lucianomlima opened this issue Feb 19, 2017 · 6 comments
Closed
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@lucianomlima
Copy link
Contributor

Description

In my app, user selects an image, makes some modifications to the image, and then uses ShareDialog to send the processed image to his Facebook account.
I can successfully share the image, but I never get the result of the promise.
I'm trying in many ways but in all of them I don't know if it was shared successfully or canceled by the user to update the user interface accordingly.

Reproduction

My sharePhotoContent object

    const { ShareDialog } = FBSDK;
    const sharePhotoContent = {
        contentType: 'photo',
        photos: [
            {
                imageUrl: imagePath,
                userGenerated: false,
                caption: "My awesome photo"
            }
        ]
    };

Below it is all the ways I tried to get result of the promise.

    ShareDialog.canShow(sharePhotoContent).then(
        function(canShow) {
            if (canShow) {
                console.log(canShow);
                return ShareDialog.show(sharePhotoContent);
            }
        }
    ).then(
        function(result) {
            console.log(result); // This never called
            result.isCancelled
            ? shareCancel = true
            : shareSuccess = true;
        },
        function(error) {
            shareError = error.toString();
        }
    );
ShareDialog.canShow(sharePhotoContent)
    .then(canShow => {
        if (canShow) {
            console.log(canShow);
            return ShareDialog.show(sharePhotoContent);
        }
    })
    .then(result => {
        console.log(result);
        result.isCancelled
            ? shareCancel = true
            : shareSuccess = true;
    })
    .catch(error => {
        shareError = error.toString();
    });
    ShareDialog.canShow(sharePhotoContent).then(
        function(canShow) {
            if (canShow) {
                console.log(canShow);
                ShareDialog.show(sharePhotoContent)
                .then(
                    function(result) {
                        console.log(result);
                        result.isCancelled
                        ? shareCancel = true
                        : shareSuccess = true;
                    },
                    function(error) {
                        shareError = error.toString();
                    }
                );
            }
        }
    );

And even trying to call ShareDialog.show directly did not work for me.

Additional Information

In iOS works perfectly.

  • React Native version: 0.39.2
  • RN FBSDK version: 0.4.0
  • Platform: Android (tested with 5.0, 5.1, 6.0)
  • Operating System: MacOS
@aakashrai1
Copy link

aakashrai1 commented Feb 22, 2017

Please try following steps:

  1. Implement ActivityEventListener in FBShareDialogModule.java

public class FBShareDialogModule extends FBSDKDialogBaseJavaModule implements ActivityEventListener

  1. Initialise CallbackManager

CallbackManager fbCallbackManager;

and in FBShareDialogModule constructor

public FBShareDialogModule(ReactApplicationContext reactContext, CallbackManager callbackManager) {
        super(reactContext, callbackManager);
        fbCallbackManager = callbackManager;  // add this line
        reactContext.addActivityEventListener(this); // add this line
    }
  1. Implement ActivityEventListener methods
    @Override
    public void onActivityResult(Activity activity,final int requestCode, final int resultCode, final Intent data) {
        fbCallbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onNewIntent(Intent intent) {}

@lucianomlima
Copy link
Contributor Author

Thanks @aakashrai1 I will test soon.

@lucianomlima
Copy link
Contributor Author

@aakashrai1 Not work for me.
Never show the ShareDialog and in log I receive the message:
ReactNativeJS: 'shareImage has been cancelled', ''

Here is FBShareDialogModule.java

package com.facebook.reactnative.androidsdk;

import android.app.Activity;
import android.content.Intent;

import com.facebook.CallbackManager;
import com.facebook.FacebookException;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.share.Sharer;
import com.facebook.share.widget.ShareDialog;

public class FBShareDialogModule extends FBSDKDialogBaseJavaModule implements ActivityEventListener {

    private CallbackManager fbCallbackManager;

    @Override
    public void onActivityResult(Activity activity, final int requestCode, final int resultCode, final Intent data) {
        fbCallbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onNewIntent(Intent intent) {}

    private class ShareDialogCallback extends ReactNativeFacebookSDKCallback<Sharer.Result> {

        public ShareDialogCallback(Promise promise) {
            super(promise);
        }

        @Override
        public void onSuccess(Sharer.Result result) {
            if (mPromise != null) {
                WritableMap shareResult = Arguments.createMap();
                shareResult.putString("postId", result.getPostId());
                mPromise.resolve(shareResult);
                mPromise = null;
            }
        }
    }

    private ShareDialog.Mode mShareDialogMode;
    private boolean mShouldFailOnError;

    public FBShareDialogModule(ReactApplicationContext reactContext, CallbackManager callbackManager) {
        super(reactContext, callbackManager);
        fbCallbackManager = callbackManager;
        reactContext.addActivityEventListener(this);
    }

    @Override
    public String getName() {
        return "FBShareDialog";
    }

    @ReactMethod
    public void canShow(ReadableMap shareContent, Promise promise) {
        if (getCurrentActivity() != null) {
            ShareDialog shareDialog = new ShareDialog(getCurrentActivity());
            promise.resolve(
                mShareDialogMode == null
                ? shareDialog.canShow(Utility.buildShareContent(shareContent))
                : shareDialog.canShow(Utility.buildShareContent(shareContent), mShareDialogMode)
            );
        } else {
            promise.reject("No current activity.");
        }
    }

    @ReactMethod
    public void show(ReadableMap shareContent, final Promise promise) {
        if (getCurrentActivity() != null) {
            ShareDialog shareDialog = new ShareDialog(getCurrentActivity());
            shareDialog.registerCallback(getCallbackManager(), new ShareDialogCallback(promise));
            shareDialog.setShouldFailOnDataError(mShouldFailOnError);
            if (mShareDialogMode != null) {
                shareDialog.show(Utility.buildShareContent(shareContent), mShareDialogMode);
            } else {
                shareDialog.show(Utility.buildShareContent(shareContent));
            }
        } else {
            promise.reject("No current activity.");
        }
    }

    @ReactMethod
    public void setMode(String mode) {
        mShareDialogMode = ShareDialog.Mode.valueOf(mode.toUpperCase());
    }

    @ReactMethod
    public void setShouldFailOnError(boolean shouldFailOnError) {
        mShouldFailOnError = shouldFailOnError;
    }
}

@sibelius
Copy link

sibelius commented Jun 8, 2017

I think https://github.com/facebook/react-native-fbsdk is a better place for this issue

@hramos
Copy link
Contributor

hramos commented Aug 16, 2017

Hi there! This issue is being closed because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally!

If you think this issue should definitely remain open, please let us know. The following information is helpful when it comes to determining if the issue should be re-opened:

  • Does the issue still reproduce on the latest release candidate? Post a comment with the version you tested.
  • If so, is there any information missing from the bug report? Post a comment with all the information required by the issue template.
  • Is there a pull request that addresses this issue? Post a comment with the PR number so we can follow up.

If you would like to work on a patch to fix the issue, contributions are very welcome! Read through the contribution guide, and feel free to hop into #react-native if you need help planning your contribution.

@hramos hramos added the Icebox label Aug 16, 2017
@hramos hramos closed this as completed Aug 16, 2017
@mahgolfa
Copy link

mahgolfa commented Jul 21, 2018

try

sharePhotoWithShareDialog() {
var tmp = this;
ShareDialog.canShow(this.state.sharePhotoContent).then(
function (canShow) {
if (canShow) {
return ShareDialog.show(tmp.state.sharePhotoContent);
}
}
).then(
function (result) {
if (result.isCancelled) {
alert('Share operation was cancelled');
} else {
alert('Share was successful with postId: '
+ result.postId);
}
},
function (error) {
alert('Share failed with error: ' + error.message);
}
);
}

@hramos hramos added the Resolution: Locked This issue was locked by the bot. label Jul 23, 2018
@facebook facebook locked as resolved and limited conversation to collaborators Jul 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

6 participants