Ay Jay
Ay Jay

Reputation: 45

Android Share to Facebook not working

I'm trying to share a content URL and send it over to facebook. I've been following the official documentation I have the dependencies in grade and I have the following in my Manifest file

        <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />

  <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name" />

  <provider android:authorities="com.facebook.app.FacebookContentProvider247315745828416"
        android:name="com.facebook.FacebookContentProvider"
        android:exported="true"/>
</application>

I can log in to my app using facebook fine. In my code I call the share content by doing the following

private void uploadToFacebook(){

    ShareLinkContent content = new ShareLinkContent.Builder()
            .setContentUrl(Uri.parse("https://developers.facebook.com"))
            .build();
    shareDialog.show(content, ShareDialog.Mode.AUTOMATIC);
    toastMessage("Done");

}

When I run the code without shareDialog.show() the toast message appears but the url is not send over to my facebook wall.

When I inclide shareDialog.show() the application crashes and I get a null pointer exception on it.

The content is definitely an object as I get a value back when I convert it to a string I get

com.facebook.share.model.ShareLinkContent@c63dc77

I am using android version 2.3.3 any help would be greatly appreciated as I can't seem to grasp what I've done wrong. I have a key hash stored for as well.

Upvotes: 2

Views: 1432

Answers (2)

Ay Jay
Ay Jay

Reputation: 45

So I found the answer.

In my on create I never added the following to the on create

shareDialog = new ShareDialog(this);

This is not in the tutorial. For all others using the tutorial create the content then in the same function the content is created below where the content is created add

shareDialog.show(content, ShareDialog.Mode.AUTOMATIC);

To share the content

Upvotes: 0

On your manifest file:

<meta-data android:name="com.facebook.sdk.ApplicationName"
        android:value="@string/app_name" />
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

    <activity android:name="com.facebook.FacebookActivity"
        android:configChanges=
            "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name" />

    <activity
        android:name="com.facebook.CustomTabActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="@string/fb_login_protocol_scheme" />
        </intent-filter>
    </activity>

I suppose you already have the internet permission on your manifest.

Then try this:

import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.LikeView;
import com.facebook.share.widget.ShareButton;

//on create

private ShareButton shareButton;
private ShareLinkContent content;

FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);

shareButton = (ShareButton) findViewById(R.id.sharing_button);
            content = new ShareLinkContent.Builder()
                    .setContentUrl(Uri.parse("your-url"))
                    .build();

            shareButton.setShareContent(content);

which should do it. Take into consideration that facebook sdk has been updated and some previous functionalities may be deprecated or not working starting from 2/5/2018. More info here.

Hope it helped.

Upvotes: 1

Related Questions