Edgar.A
Edgar.A

Reputation: 1383

ShareDialog doesn't open facebook app

Trying to implement facebook sharing, however it only works in webview if I set ShareDialog.Mode.AUTOMATIC, if I set it to NATIVE then nothing shows up at all and I don't receive no error callback. Facebook app is installed in the emulator.

Provider in manifest:

<provider android:authorities="com.facebook.app.FacebookContentProvider{myAppId}"
        android:name="com.facebook.FacebookContentProvider"
        android:exported="true"/>

Code for sharing:

public void share(String contentUrl,
                  Activity activity){
        ShareLinkContent content = new ShareLinkContent.Builder()
            .setContentUrl(Uri.parse(contentUrl))
            .build();
    ShareDialog dialog = new ShareDialog(activity);
    dialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Sharer.Result result) {
                EventBus.getDefault().post(new ShareEvent(true));
        }

        @Override
        public void onCancel() {
            Log.e(TAG, "Facebook Share Cancel");
            EventBus.getDefault().post(new ShareEvent(false));
        }

        @Override
        public void onError(FacebookException error) {
            Log.e(TAG, "Facebook share error: " + error.getMessage());
            EventBus.getDefault().post(new ShareEvent(false));
        }
    })
    dialog.show(content, ShareDialog.Mode.NATIVE);
}

I'm calling share(..) method from fragment.

I tried implementing sharing using intents, and that works

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://myUrl.com");
    PackageManager pm = activity.getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
    for (final ResolveInfo app : activityList) {
        if ((app.activityInfo.name).contains("facebook")) {
            final ActivityInfo mActivity = app.activityInfo;
            final ComponentName name = new ComponentName(mActivity.applicationInfo.packageName, mActivity.name);
            shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            shareIntent.setComponent(name);
            activity.startActivity(shareIntent);
            break;
        }
    }

However in my case I need successful share callback, which as much as I know my last solution doesn't provide, since it always return result CANCELED

Upvotes: 2

Views: 1340

Answers (1)

SGX
SGX

Reputation: 3353

Have you declared permission for it? Like below:

FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();

List<String> permissionNeeds = Arrays.asList("publish_actions"); // permission.
loginManager = LoginManager.getInstance();

loginManager.logInWithPublishPermissions(this, permissionNeeds);

loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
      @Override
      public void onSuccess(LoginResult loginResult) {
             // do something here
             finish();
      }

      @Override
      public void onCancel() {}

      @Override
      public void onError(FacebookException exception) {}
});

This is my sample code: https://github.com/minhhuy150894/simple-puzzle/blob/master/app/src/main/java/xyz/davidng/puzzle/FacebookShareImage.java

Upvotes: 1

Related Questions