Share Score on Facebook android

2019-01-14 21:19发布

I want to share my game score on facebook. I checked lots of links and every post people are saying that POSTING on facebook using INTENT is not possible.If we are using intent than we can share only link.

If we have to share something on facebook than we have to use FaceBook SDK.

I have another doubt that all questions and answers were posted before Year 2014. Is any new thing come after year 2014.

My actual question is that, Is it possible to share score on Facebook using Intent or i have to use Facebook SDK ?

below is the intent code which i used for my application which is not working ......

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, message);
        startActivity(Intent.createChooser(intent, "Share on"));

and below is the FacebookSDK code ...and Problem in this it is not showing score on the Post only link image is showing, and title & description is missing.

  FacebookSdk.sdkInitialize(getApplicationContext());

            shareDialog = new ShareDialog(this);
             if (ShareDialog.canShow(ShareLinkContent.class)) {
                        linkContent = new ShareLinkContent.Builder()
                                .setContentTitle(title)
                                .setContentDescription(description)
                                .setContentUrl(Uri.parse(link)).
                                .setImageUrl(Uri.parse(imageLink)   
                                .build();

                        shareDialog.show(linkContent);
                    }

I used ShareDialog because

The Share dialog switches to the native Facebook for Android app, then returns control to your app after a post is published. If the Facebook app is not installed it will automatically fallback to the web-based dialog.

Below is the output ..........

output

@pravin this is error coming after your share API use

o

@Pravin this is my code of your share answer........

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        setContentView(R.layout.activity_facebook);

        Button mShare= (Button) findViewById(R.id.share);

        mShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
                        .putString("og:type", "game.achievement")
                        .putString("og:title", "Name of your game")
                        .putString("og:description", "description. You can share your score here")
                        .putString("game:points", "445")
                        .build();

                ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
                        .setActionType("games.achieves")
                        .putObject("game", object)
                        .build();




                if (ShareDialog.canShow(ShareLinkContent.class)) {
                    ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
                            .setPreviewPropertyName("game")
                            .setAction(action)
                            .build();

                    ShareDialog.show(Facebook.this, content);
                }
            }
        });
        shareDialog = new ShareDialog(this);

    }

Thanx in advance ................

6条回答
够拽才男人
2楼-- · 2019-01-14 21:42

Well, there are 3 things you should do :
1) Categorize your app as Games on Facebook Developers
2) Use the Achievements API and Scores API
3) Facebook for Developers officially has an answer to your question. Please refer to the link https://developers.facebook.com/docs/games/services/scores-achievements

[ OPTIONAL ]


If you want to study about General Sharing from your app by users than this link Facebook SharingAddit might also help you in as General Sharing of content from your app

查看更多
姐就是有狂的资本
3楼-- · 2019-01-14 21:44

You have to use Facebook SDK to do this, I think the main reason is about security.

This is a simple snippet for photo sharing example (not related to your question but i think you know you can't not do it by using Intent):

private void shareImage() {
        SharePhoto photo = new SharePhoto.Builder()
                .setBitmap(bmp)
                .setCaption("Simple puzzle")
                .build();

        SharePhotoContent content = new SharePhotoContent.Builder()
                .addPhoto(photo)
                .build();

        ShareApi.share(content, null);
    }
查看更多
SAY GOODBYE
4楼-- · 2019-01-14 21:50

Just try the below snippet,

call wherever you want like below

ShareDialog shareDialog;
FacebookSdk.sdkInitialize(mContext);
shareDialog = new ShareDialog(mContext);

shareScoreonFB("345"); // just pass your score here in string format. call this inside click listener.

// GLOBAL FUNCTION TO SHARE SCORE ON FB 
void shareScoreonFB(String score)
{
ShareLinkContent linkContent = new ShareLinkContent.Builder()
                    .setContentTitle("Your score is : "+score)
                    .setContentDescription("Any description that you needed")
                    .setContentUrl(Uri.parse("https://play.google.com/store/apps/details?id=com.mobtraffiq.numbertap&hl=en")).build();
            shareDialog.show(linkContent);
}

NOTE: Make sure you initialize the Facebook SDK.

Hope this helps you, let me know for queries.

查看更多
Fickle 薄情
5楼-- · 2019-01-14 21:55

Scores API

If you just want to share score you can use Scores API(Read Create or Update a Player's Score section). There is very less and unclear information in provided link so below code snippet might work for you.

    Bundle param = new Bundle();
    param.putInt("score", 100);

    new GraphRequest(
    your_access_token,
    "/USER_ID/scores",
    param,
    HttpMethod.POST,
    new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            /* handle the result */
        }
    }
).executeAsync();

Achievements API

Use Achievements API if you want to publish achievements in your game and reward players with scores for completing those achievements.You have to use Open Graph Stories to publish your game achievements on Facebook. Before publishing stories on behalf of people, You will have to submit them for review. Read this link for more information.There are two ways to publish an Open Graph story:

  1. Using the Share dialog
  2. Using your own custom interface

As you are using share dialog, I am going to throw some light on how to post game score using share dialog.

  1. Create an object with the object type game.achievement and set the properties on that object.
ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
        .putString("og:type", "game.achievement")
        .putString("title", "Title of the achievement type.")
        .putString("og:description", "description. You can share your score here")
        .putInt("game:points", Points in integer)
  .putString("og:url", "url of achievement type") 
  .putString("og:image", "image url") 
  .putString("fb:app_id", "The App ID that owns this achievement type") 
  .build();

In above code snippet key is Object Properties and game.achievement is Object Type. Here is documentation for game:points.

  1. Then create an action and link the object to the action.
ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
    .setActionType("games.achieves")
    .putObject("game", object)
    .build();

You can find different ActionTypes here.

  1. Finally, create the content model to represent the Open Graph story.
ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
    .setPreviewPropertyName("game")
    .setAction(action)
    .build();

All objects and action types in your code must be lowercase. All property names require namespaces.

  1. Present the Share dialog by using the show method on ShareDialog.

ShareDialog.show(activityOrFragment, content);

Summary:-

The Scores API and Achievements API make it easy to build social leaderboards, persist player scores and award achievements to players in a social and cross-platform format. With the Scores API, you can store and reset a player's high-score and pull back a list of their friends' scores to populate a leaderboard. With the Achievements API, you can define a list of custom achievements that players can complete in your game, along with a score value that can be earned with each achievement.

Note:-

Both the Scores API and Achievements API are only available for apps that are categorized as Games.

Because these API let developers write information about player game state to the Graph API, they both require publish_actions permissions, which is subject to Login Review. To retrieve Score and Achievement information about a player's friends, both the player and their friends need to grant the user_friends permission.

Hope this will provide you and other users enough information on game integration with Facebook.

查看更多
混吃等死
6楼-- · 2019-01-14 21:57

I believe that the Facebook SDK uses a background Activity for the actual data transfer, but you still need to make your own activity, its layout, and the examples on here show you how to create your own Sharing and Login tools

查看更多
smile是对你的礼貌
7楼-- · 2019-01-14 21:58

After long searching i found this as work for me in my Project till the actual answer will come ..........

            FacebookSdk.sdkInitialize(getApplicationContext());

            shareDialog = new ShareDialog(this);
            if (ShareDialog.canShow(ShareLinkContent.class)) {
                    linkContent = new ShareLinkContent.Builder()
                        .setQuote("Hi Guys I have completed Game 30 in 19 seconds  in this game")
                        .setImageUrl(Uri.parse("https://lh3.googleusercontent.com/jUej7mN6M6iulmuwmW6Mk28PrzXgl-Ebn-MpTmkmwtOfj5f0hvnuw8j0NEzK0GuKoDE=w300-rw"))
                        .setContentUrl(Uri.parse("https://play.google.com/store/apps/details?id=com.mobtraffiq.numbertap&hl=en"))
                        .build();

                    shareDialog.show(linkContent);
                }

NOTE:-In this code its shared a quota. If any one get other answer please post with output.

Output:-

output

Please post guys if you get the actual answer of this question ....

Thank you for all the user who make their effort .......

I have learn many thing due to your answers .......

enjoy coding .........

查看更多
登录 后发表回答