6

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 request to others.

Can anyone help me out?

here is my code snippet:

Bundle params = new Bundle();
params.putString("message", getString(R.string.request_message));
Utility.mFacebook.dialog(Hackbook.this, "apprequests", params, new AppRequestsListener());

and AppRequestsListener:

public class AppRequestsListener extends BaseDialogListener {
    @Override
    public void onComplete(Bundle values) {
        Toast toast = Toast.makeText(getApplicationContext(), "App request sent", Toast.LENGTH_SHORT);
        toast.show();
    }

    @Override
    public void onFacebookError(FacebookError error) {
        Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancel() {
        Toast toast = Toast.makeText(getApplicationContext(), "App request cancelled", Toast.LENGTH_SHORT);
        toast.show();
    }
}
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
Prachi
  • 2,559
  • 4
  • 25
  • 37
  • No need for the code from facebook sdk, just yours.. What is this *AppRequestsListener*? Can you please add that code as well. – Nitzan Tomer Apr 16 '12 at 07:15
  • @NitzanTomer AppRequestsListener is just a callback listener ..just to show toast.somehow can we intercept the request and check its parameter ??? – Prachi Apr 16 '12 at 08:46
  • @NitzanTomer Hackbook is my class name can you post your InviteListener's code as in my case i,m using AppRequestsListener to show a toast – Prachi Apr 16 '12 at 09:34
  • My Listener is pretty simple in functionality but there's a hierarchy there which will result in me posting the code of at least 3 classes for you to get what I'm doing there. Because of that, it will be simpler if you post your code, I'm trying to help you out here but you don't seem to want to get help. I'll only ask this once more and then will move on: please, share the code of your *AppRequestsListener* regardless of what you think it does or doesn't do. – Nitzan Tomer Apr 16 '12 at 09:44
  • @NitzanTomer hey i have edited my code just check it out – Prachi Apr 16 '12 at 09:55
  • Your code seems a ok, so if you get the *"App request sent"* message then it was probably sent. Have you tried sending it to more than one user? Maybe you sent it to a user which blocked invites from the account you are using to send the invites? Are you working on a real device or emulator? – Nitzan Tomer Apr 16 '12 at 10:02
  • i'm using real device and try to send app request to multiple users – Prachi Apr 16 '12 at 10:06
  • @NitzanTomer do i need to give some permissions?????? – Prachi Apr 16 '12 at 10:15
  • No, there's no permission requirements for this. The only thing I can think of that might can help is to check the android logs, do you have Logcat (or any equivalent) installed? Check out the logs, maybe something went wrong? – Nitzan Tomer Apr 16 '12 at 10:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10119/discussion-between-curious-mind-and-nitzan-tomer) – Prachi Apr 16 '12 at 10:32
  • @NitzanTomer do i have to use canvas and open graph api for this??? – Prachi Apr 17 '12 at 05:30

6 Answers6

9

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:

private void sendRequestDialog() {
    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
                public void onComplete(Bundle values, FacebookException error) {
                    // do something, e.g. show toast message
                }   
            })
            .build();
    requestsDialog.show();
}

Reference: Facebook SDK 3.0 for Android: Send Requests

rlay3
  • 10,002
  • 6
  • 30
  • 22
4

Using Facebook Api 3.0

1. Send friend request

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

RequestsDialogBuilder builder = new RequestsDialogBuilder(MainActivity.this,
                                    Session.getActiveSession(), params);

builder.setOnCompleteListener(new OnCompleteListener() {

    @Override
    public void onComplete(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{

            final String requestId = 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();
            }
        }
    }
});

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

2. Send app request

Bundle parameters = new Bundle();
parameters.putString("message", "Send Request");

WebDialog.Builder builder = new Builder(MainActivity.this, Session.getActiveSession(),
                                "apprequests", parameters);

builder.setOnCompleteListener(new OnCompleteListener() {

    @Override
    public void onComplete(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{

            final String requestId = 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();
            }
        }                       
    }
});

WebDialog webDialog = builder.build();
webDialog.show();
Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80
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 = new Facebook("YOUR_APP_ID");

....

Bundle params = new Bundle();
params.putString("title", "invite friends");
facebook.dialog(this, "apprequests", params, new DialogListener() {
    @Override
    public void onComplete(Bundle values) {}

    @Override
    public void onFacebookError(FacebookError error) {}

    @Override
    public void onError(DialogError e) {}

    @Override
    public void onCancel() {}
});

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?

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • can you elaborate little bit as i try to use above code but no success – Prachi Apr 14 '12 at 12:46
  • I think it's pretty straight forward.. It's obviously just a "skeleton" code snippet, you'll have to implement the *DialogListener* and such. What do you mean by "no success"? Do you get an error? If so what is it? Also, be sure to read the documentation (I linked to 3 different fb documentation pages). I'll be happy to help you more but I need to know what you're missing/what's not working. – Nitzan Tomer Apr 14 '12 at 12:49
  • thnx for reply..I have already read the documentation and used the Facebook sdk provided by Facebook and followed the steps as mentioned in documentation .By using hackbook's source code i am able to post message on wall etc but can not send app request to other Facebook friends i mean its shows me that my app request was send but actually other user not got that request.can you please help me out with this problem ...thanx in adv – Prachi Apr 15 '12 at 17:55
  • What do you mean by "shows me that my app request was send"? How does it show that? Also, can you please edit your question with the code you are using? – Nitzan Tomer Apr 15 '12 at 19:44
  • i'm using project and library [link](https://github.com/facebook/facebook-android-sdk)as it is just modified app id with my app id.After selecting friends to send an app request a toast appears which says app request send but actually other user not got that request. – Prachi Apr 16 '12 at 05:34
  • I can't really help if I can't see the code. Edit your original question, and add to it the code you are using for the apprequests dialog. – Nitzan Tomer Apr 16 '12 at 05:38
  • This is with the old sdk. Anybody knows how to answer this for SDK 3? – rds Nov 27 '12 at 21:45
  • 3
    For SDK v3, `Facebook.dialog()` is deprecated and replaced by [`WebDialog`](https://developers.facebook.com/docs/reference/android/3.0/WebDialog) – rds Nov 27 '12 at 21:58
2

this code working fine for me in Facebook Sdk 3.0

 private void sendRequestDialog(final String userId) {
         Bundle params = new Bundle();       
         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 id

         WebDialog requestsDialog = ( new WebDialog.RequestsDialogBuilder(MainActivity.this,
                 Session.getActiveSession(), params)).setOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(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 {
                    final String requestId = 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();
     }
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
  • 1
    I try this but it says request sent but no notification comes to friend that i have selected.. – Pranav Jun 18 '14 at 10:50
0

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.

public void onComplete(Bundle values) {
    final String returnId = 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.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
0

Using Facebook Api < 3.0 - Send app request

reference: https://stackoverflow.com/users/1237937/kirit

public void run() {
            Bundle parameters = new Bundle();
            parameters.putString("message", "Send Request");

        WebDialog.Builder builder = new WebDialog.Builder(ConnectionSearchFacebook.this, facebookConnector.getFacebook().getSession(),
                "apprequests", parameters);

        builder.setOnCompleteListener(new WebDialog.OnCompleteListener() {
            @Override
            public void onComplete(Bundle values, FacebookException error) {
                if (error != null){
                    if (error instanceof FacebookOperationCanceledException){
                        Toast.makeText(ConnectionSearchFacebook.this,"Request cancelled",Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(ConnectionSearchFacebook.this,"Network Error",Toast.LENGTH_SHORT).show();
                    }
                }
                else{

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

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

    }
Community
  • 1
  • 1
PhuocLuong
  • 699
  • 9
  • 18
  • I try this but it says request sent but no notification comes to friend that i have selected.. – Pranav Jun 18 '14 at 10:50