Facebook Developers
DocsToolsSupportNewsApps
Log In
  • Social Plugins
  • Facebook Login
  • Open Graph
  • Facebook APIs
  • Games
  • Payments
  • App Center
  • Promote Your App
  • iOS
  • Android
  • JavaScript
  • PHP
  • More SDKs
  • Topics
    • Facebook SDK for Android
  • Concepts
    • Build, Distribute and Promote
  • Features
    • What's New in 3.0
    • Mobile App Install Ads
  • Getting Started
    • Getting Started with the Facebook SDK for Android
  • Tutorials
    • Facebook SDK for Android Tutorial
    • 1 - Authenticate with Facebook Login
    • 2 - Personalize
    • 3 - Show Friends
    • 4 - Show Nearby Places
    • 5 - Publish Open Graph Story
    • Upgrading from 2.0 to 3.0
    • Upgrading from 3.0.2.b to 3.0
    • Mobile App Install Ads
  • Games Tutorial
    • Android Games Tutorial
    • 1 - Authenticate
    • 2 - Personalize
    • 3 - Invites and Requests
    • 4 - Bragging and News Feed
    • 5 - Publish Open Graph Story
  • How Tos
    • Facebook Login Flow for Android
    • Send Requests
    • Handle Request App Links
    • Use the Feed Dialog
    • Fetch User Data
    • Link To Your Native App
    • Publish to Feed
    • Run FQL Queries
    • Use Batch Requests
    • Use the Object API
    • Use the native Login Dialog
  • Reference
    • Facebook SDK for Android Reference
  • Other Resources
    • Android Change Log 3.x
    • Facebook SDK for Android Downloads

Send Requests

Documentation › Send Requests

Social channels let users post to news feed or send requests to their friends. The Facebook SDK for Android provides a method for integrating social channels with Facebook dialogs, including the Requests Dialog that people use to send notifications to their friends.

The Requests Dialog lets you provide basic Facebook functionality in your app with a few lines of code. There's no need to build native dialogs, make API calls or handle responses.

The Requests Dialog is supported through the WebDialog class that contains a WebDialog.RequestsDialogBuilder constructor that you can use to build the dialog. You pass in additional parameters that describe the request then call the show() method to display the dialog.

This document walks you through the following:

  • Prerequisites
  • Sample Overview
  • Step 1: Set Up the User Interface
  • Step 2: Send the Request
  • Step 3: Send Additional Data
  • Additional Info

Prerequisites

Before you begin, make sure you've already set up Facebook Login. This ensures you have the prerequisites and your app is ready for additional Facebook integration.


Sample Overview

The completed sample allows users to log in with Facebook and send a request to one or more friends.

The implementation builds on top of Facebook Login, adding a button that opens up a Requests Dialog where the user can select one or more friends and send a request. When the user sends the request, an alert pops up confirming that the request was sent. Friends using Facebook for Android should see the request notification.


Step 1: Set Up the User Interface

In this step, you'll add a button in the initial layout that launches the Requests Dialog.

First, define a new string resource for the send request button. Open the Android string resource file, res/values/strings.xml, and define a new string:

<string name="send_request">Send Request</string>

Then, open your main activity's layout XML file, res/layout/main.xml. Add a <Button> to the layout just below the login button:

<Button
    android:id="@+id/sendRequestButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:gravity="center"
    android:layout_marginTop="30dp"
    android:visibility="invisible"
    android:text="@string/send_request"
    />

The button will be hidden initially.

Now, open your fragment class, MainFragment.java and declare a private variable for the button:

private Button sendRequestButton;

If you followed the Facebook Login doc, you should have a onSessionStateChange() method in your MainFragment class file that controls the logged-in and logged-out user interface. Modify this method to show the send request button only when the user is authenticated:

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        sendRequestButton.setVisibility(View.VISIBLE);
    } else if (state.isClosed()) {
        sendRequestButton.setVisibility(View.INVISIBLE);
    }
}

Step 2: Send the Request

In this step, you'll add the logic to send the request.

Open up the MainFragment class and define a new private method that invokes the Requests Dialog:

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

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

                @Override
                public void onComplete(Bundle values,
                    FacebookException error) {
                    if (error != null) {
                        if (error instanceof FacebookOperationCanceledException) {
                            Toast.makeText(getActivity().getApplicationContext(), 
                                "Request cancelled", 
                                Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getActivity().getApplicationContext(), 
                                "Network Error", 
                                Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        final String requestId = values.getString("request");
                        if (requestId != null) {
                            Toast.makeText(getActivity().getApplicationContext(), 
                                "Request sent",  
                                Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getActivity().getApplicationContext(), 
                                "Request cancelled", 
                                Toast.LENGTH_SHORT).show();
                        }
                    }   
                }

            })
            .build();
    requestsDialog.show();
}

Then, implement the send request button's click handler to call the method you defined to invoke the Requests Dialog. Do this by modifying the onCreateView() method and adding the button click handler code:

sendRequestButton = (Button) view.findViewById(R.id.sendRequestButton);
sendRequestButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        sendRequestDialog();        
    }
});

Build and run the project to make sure it runs without errors. Tap the ''Log In'' button to log in with Facebook. Once authenticated, tap ''Send Request'' and verify the Requests Dialog displays. Choose one or more friends and click ''Send'' to send the request. You should not see an alert confirming the request was sent. Test the dialog cancel flow as well, which happens when the user clicks the ''x'' icon or clicks the Cancel button.

Ideally, you should send a request to a friend or test user who can confirm the notification receipt. Friends using Facebook for Android should see the notification. Friends should also see the notification in their App Center Requests on desktop, where they can delete the notification.




Step 3: Send Additional Data

In this step, you'll modify your request to send additional data.

You can pass arbitrary data as an additional parameter when making the dialog request call. In this sample, you'll modify the params bundle that is passed into the WebDialog.RequestsDialogBuilder() method by adding a data key that contains the extra data. This data can later be retrieved through a Graph API call to the request ID that's generated when the request is sent.

Open your MainFragment class and modify the sendRequestDialog() method to pass in the extra data. You'll pass in two arbitrary pieces of info in the data, one called badge_of_awesomeness and a second called social_karma. This could represent a gift that the app user is sending to their friend. Make the following code changes:

....
params.putString("message", "Learn how to make your Android apps social");
params.putString("data",
        "{\"badge_of_awesomeness\":\"1\"," +
        "\"social_karma\":\"5\"}");

Build and run the project to make sure it runs without errors. Once authenticated, tap ''Send Request'' and choose a few friends in the dialog. Then, click ''Send'' to send the request. You shouldn't see an alert confirming the request was sent.

See how to process the data when a friend clicks on the notification in the next doc: Handle Request App Links.


Additional Info

  • Requests Dialog feature documentation
  • WebDialog.RequestsDialogBuilder API reference
Updated 7 hours ago
Facebook © 2013 · English (US)
AboutAdvertisingCareersPlatform PoliciesPrivacy Policy