Skip to content Skip to sidebar Skip to footer

How To Send App Requests To Friends Through Facebook Android Sdk

Currently I'm developing an Android app for that I'm using the Facebook SDK. It's working fine for posting messages to the wall etc., but through this SDK I'm unable to send an app

Solution 1:

As of SDK version 3.0 you use a WebDialog. Here is an example of how to create one using the provided Builder that uses all of the available parameters:

privatevoidsendRequestDialog() {
    Bundle params = new Bundle();
    params.putString("title", "Send a Request");
    params.putString("message", "Learn how to make your Android apps social");
    params.putString("to", "12343543,32423534");  // comma seperated list of facebook IDs to preset the recipients. If left out, it will show a Friend Picker.params.putString("data",
        "{\"badge_of_awesomeness\":\"1\"," +
        "\"social_karma\":\"5\"}");  // any additional data

    WebDialog requestsDialog = (
        new WebDialog.RequestsDialogBuilder(getActivity(), Session.getActiveSession(), params))
            .setOnCompleteListener(new OnCompleteListener() {

                @Override
                publicvoidonComplete(Bundle values, FacebookException error) {
                    // do something, e.g. show toast message
                }   
            })
            .build();
    requestsDialog.show();
}

Reference: Facebook SDK 3.0 for Android: Send Requests

Solution 2:

Using Facebook Api 3.0

1. Send friend request

Bundleparams=newBundle();
params.putString("message", "Learn how to make your Android apps social");

RequestsDialogBuilderbuilder=newRequestsDialogBuilder(MainActivity.this,
                                    Session.getActiveSession(), params);

builder.setOnCompleteListener(newOnCompleteListener() {

    @OverridepublicvoidonComplete(Bundle values, FacebookException error) {

        if (error != null){
            if (error instanceof FacebookOperationCanceledException){
                Toast.makeText(MainActivity.this,"Request cancelled",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(MainActivity.this,"Network Error",Toast.LENGTH_SHORT).show();
            }
        }
        else{

            finalStringrequestId= values.getString("request");
            if (requestId != null) {
                Toast.makeText(MainActivity.this,"Request sent",Toast.LENGTH_SHORT).show();
            } 
            else {
                Toast.makeText(MainActivity.this,"Request cancelled",Toast.LENGTH_SHORT).show();
            }
        }
    }
});

WebDialogrequestDialog= builder.build();
requestDialog.show();

2. Send app request

Bundleparameters=newBundle();
parameters.putString("message", "Send Request");

WebDialog.Builderbuilder=newBuilder(MainActivity.this, Session.getActiveSession(),
                                "apprequests", parameters);

builder.setOnCompleteListener(newOnCompleteListener() {

    @OverridepublicvoidonComplete(Bundle values, FacebookException error) {
        if (error != null){
            if (error instanceof FacebookOperationCanceledException){
                Toast.makeText(MainActivity.this,"Request cancelled",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(MainActivity.this,"Network Error",Toast.LENGTH_SHORT).show();
            }
        }
        else{

            finalStringrequestId= values.getString("request");
            if (requestId != null) {
                Toast.makeText(MainActivity.this,"Request sent",Toast.LENGTH_SHORT).show();
            } 
            else {
                Toast.makeText(MainActivity.this,"Request cancelled",Toast.LENGTH_SHORT).show();
            }
        }                       
    }
});

WebDialogwebDialog= builder.build();
webDialog.show();

Solution 3:

The android sdk has Dialogs which you can use, and when you open a dialog you specify which dialog you want to open.

You can see the list of available dialogs in the Dialogs documentation. One of the dialogs is the Requests Dialog and you can open that from the android sdk as well, something like:

Facebook facebook = newFacebook("YOUR_APP_ID");

....

Bundle params = newBundle();
params.putString("title", "invite friends");
facebook.dialog(this, "apprequests", params, newDialogListener() {
    @OverridepublicvoidonComplete(Bundle values) {}

    @OverridepublicvoidonFacebookError(FacebookError error) {}

    @OverridepublicvoidonError(DialogError e) {}

    @OverridepublicvoidonCancel() {}
});

You can add more parameters for this dialog, use the documentation to see what it is you need.


Edit

Ok, check out this code:

Bundle paramsOut = new Bundle(), paramsIn = this.getIntent().getExtras();
paramsOut.putString("message", paramsIn.getString("message"));
this.facebook.dialog(this, "apprequests", paramsOut, new InviteListener(this));

I use it and it works well for me, the app request is being sent and the user receives it. Since your code is pretty similar, it is safe to assume that the problem is with what's different, and so you should post the code to what is different.

So, what's in that AppRequestsListener of yours? Saying that it just shows a popup does not help me to help you. Also, what is this *Hackbook"? is it an activity?

Solution 4:

this code working fine for me in Facebook Sdk 3.0

privatevoidsendRequestDialog(final String userId) {
         Bundleparams=newBundle();       
         params.putString("title", "Invite Friend");
         params.putString("message", "has invite has you to try out " +
                 "The perfect gamified experience for today's smart and social Cricket fan " +
                 "Download now on your ANDROID device!");
         // comma seperated list of facebook IDs to preset the recipients. If left out, it will show a Friend Picker.
         params.putString("to",  userId);  // your friend idWebDialogrequestsDialog= ( newWebDialog.RequestsDialogBuilder(MainActivity.this,
                 Session.getActiveSession(), params)).setOnCompleteListener(newOnCompleteListener() {
            @OverridepublicvoidonComplete(Bundle values, FacebookException error) {
                //   Auto-generated method stub                     if (error != null) {
                    if (error instanceof FacebookOperationCanceledException) {
                        Toast.makeText(MainActivity.this.getApplicationContext(), 
                                "Request cancelled", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this.getApplicationContext(), 
                                "Network Error",  Toast.LENGTH_SHORT).show();
                    }
                } else {
                    finalStringrequestId= values.getString("request");
                    if (requestId != null) {
                        Toast.makeText(MainActivity.this.getApplicationContext(), 
                                "Request sent",  Toast.LENGTH_SHORT).show();
                        Log.i("TAG", " onComplete req dia ");                                   
                    } else {
                        Toast.makeText(MainActivity.this.getApplicationContext(), 
                                "Request cancelled", Toast.LENGTH_SHORT).show();
                    }
                }                   
            }
         }).build();
         requestsDialog.show();
     }

Solution 5:

I think you are missing the code in onComplete where you actually send the request. All you have in onComplete is a toast that is why you receive a message saying the request was sent. You need a return Id to actually send the request.

publicvoidonComplete(Bundle values) {
    finalStringreturnId= values.getString("request");

    if (returnId != null) {
        Toast.makeText(getApplicationContext(),
                       "Request sent " + returnId,
                       Toast.LENGTH_SHORT).show();
    }
}

You have to actually send out the values onComplete.

Post a Comment for "How To Send App Requests To Friends Through Facebook Android Sdk"