Android: send facebook message with custom actions

I want to send a message to facebook with a custom action, which is a hyperlink that appears next to similar links and comments.

How can I do that?

thank

+2
source share
4 answers

You must add "actions" to the request parameters:

JSONStringer actions;
try {
    actions = new JSONStringer().object()
                .key("name").value("Click me!")
                .key("link").value("http://stackoverflow.com/").endObject();
     params.putString("actions", actions.toString());
} catch (JSONException e) {
    e.printStackTrace();
}
+3
source

I prefer to copy the source code of the Facebook SDK into my own Android Eclipse project to make debugging easier. My code is based on a simple example that comes with the Facebook SDK. Therefore, if you want to copy my code, be sure to add some classes from the sample project.

JSONObject , . , , , . , .


    private void publishPhoto(String imageURL) {
        Log.d("FACEBOOK", "Post to Facebook!");

        try {

            JSONObject attachment = new JSONObject();
            attachment.put("message", Utils.s(R.string.fb_message));
            attachment.put("name", Utils.s(R.string.fb_name));
            attachment.put("href", Utils.s(R.string.url_dotzmag));
            attachment.put("description", Utils.s(R.string.fb_description));

            JSONObject media = new JSONObject();
            media.put("type", "image");
            media.put("src", imageURL);
            media.put("href", Utils.s(R.string.url_dotzmag));
            attachment.put("media", new JSONArray().put(media));

            JSONObject properties = new JSONObject();

            JSONObject prop1 = new JSONObject();
            prop1.put("text", "Dotz App on Android Market");
            prop1.put("href", Utils.s(R.string.url_android_market));
            properties.put("Get the App for free", prop1);

            JSONObject prop2 = new JSONObject();
            prop2.put("text", "Dotz Tuning on Facebook");
            prop2.put("href", Utils.s(R.string.url_facebook_fanpage));
            properties.put("Visit our fanpage", prop2);

            attachment.put("properties", properties);

            Log.d("FACEBOOK", attachment.toString());

            Bundle params = new Bundle();
            params.putString("attachment", attachment.toString());
            mFacebook.dialog(mActivity, "stream.publish", params, new PostPhotoDialogListener());
            //mAsyncRunner.request("me/feed", params, "POST", new WallPostRequestListener(), null);

        } catch (JSONException e) {
            Log.e("FACEBOOK", e.getLocalizedMessage(), e);
        }
    }

    public class PostPhotoDialogListener extends BaseDialogListener {

        public void onComplete(Bundle values) {
            final String postId = values.getString("post_id");
            if (postId != null) {
                Log.d("FACEBOOK", "Dialog Success! post_id=" + postId);
                Toast.makeText(mActivity, "Successfully shared on Facebook!", Toast.LENGTH_LONG).show();
                /*
                mAsyncRunner.request(postId, new WallPostRequestListener());
                mDeleteButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        mAsyncRunner.request(postId, new Bundle(), "DELETE",
                                new WallPostDeleteListener(), null);
                    }
                });
                */
            } else {
                Log.d("FACEBOOK", "No wall post made");
            }
        }
    }
+6
private void publishPhoto(String imageURL) {
    Log.d("FACEBOOK", "Post to Facebook!");

    try {

        JSONObject attachment = new JSONObject();
        attachment.put("message","Type your message to share");
        attachment.put("name", "Your Application Name"));
        attachment.put("href", "Any hyperLink");
        attachment.put("description","Description about Application");

        JSONObject media = new JSONObject();
        media.put("type", "image");
        media.put("src",  "URL path of posting image");
        media.put("href","Any hyperLink"));
        attachment.put("media", new JSONArray().put(media));

        JSONObject properties = new JSONObject();

        JSONObject prop1 = new JSONObject();
        prop1.put("text", "Text or captionText to Post");
        prop1.put("href", "Any hyperLink");
        properties.put("Get the App for free(or any custom message))", prop1);

        // u can make any number of prop object and put on "properties" for    ex:    //prop2,prop3

        attachment.put("properties", properties);

        Log.d("FACEBOOK", attachment.toString());

        Bundle params = new Bundle();
        params.putString("attachment", attachment.toString());
        mFacebook.dialog(mActivity, "stream.publish", params, new PostPhotoDialogListener());      

    } catch (JSONException e) {
        Log.e("FACEBOOK", e.getLocalizedMessage(), e);
    }
}

public class PostPhotoDialogListener extends BaseDialogListener {

    public void onComplete(Bundle values) {
        final String postId = values.getString("post_id");
        if (postId != null) {
            Log.d("FACEBOOK", "Dialog Success! post_id=" + postId);
            Toast.makeText(mActivity, "Successfully shared on Facebook!", Toast.LENGTH_LONG).show();

        } else {
            Log.d("FACEBOOK", "No wall post made");
        }
    }
}
+1

facebook:

http://restfb.com/

, JSON; -)

:

// Publishing an image to a photo album is easy!
// Just specify the image you'd like to upload and RestFB will handle it from there.

FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
  BinaryAttachment.with("cat.png", getClass().getResourceAsStream("/cat.png")),
  Parameter.with("message", "Test cat"));

out.println("Published photo ID: " + publishPhotoResponse.getId());

// Publishing a video works the same way.

facebookClient.publish("me/videos", FacebookType.class,
  BinaryAttachment.with("cat.mov", getClass().getResourceAsStream("/cat.mov")),
  Parameter.with("message", "Test cat"));
0

All Articles