Social channels let users post to news feed or send requests to their friends. The Facebook SDK for Android provides a method to integrate social channels with Facebook dialogs, including the Requests Dialog that people use to send notifications to their friends.
The Send Requests doc walks you through how to build requests into your app. When someone receives a request from their friends on the Facebook for Android app, if they tap the notification, they'll be directed to your app if you configured your App Dashboard settings.
To give the user a good, relevant experience, process the incoming notifications when your app is opened. For example, the user may have sent a gift as part of the notification. Your app may contain a view that displays incoming gifts. When the friend opens the app, direct them to the gifts view.
Notifications are kept on Facebook for two weeks, until they automatically time out. You should proactively delete notifications when you're done processing them.
This document walks you through the following:
Before you begin, make sure you've gone through the Send Requests doc and implemented the Requests dialog.
To properly handle native app linking, make the following configuration changes in the App Dashboard:
Package Name: This is the unique app identifier used by Android to open your app if it's installed. This is often of the format com.domain.appname. You can find this in your AndroidManifest.xml file. Look for the package= attribute of the manifest element and paste it here.
Class Name:: This is the class name of the main activity you want Facebook to launch. Include the namespace of your app (ex: com.domain.appname.MainActivity). This field is also used to generate the Google Play Store URL.
Key Hash: Enter your app signature or key hash.
Facebook Login: Enable this setting.

The completed sample allows users to tap on a request notification from Facebook for Android, then log in with Facebook if necessary to handle the incoming notification. A confirmation for the notification displays. The notification is then deleted and the deletion is confirmed.
The implementation builds on top of Send Requests, adding logic to detect the incoming notification, show the notification and then clear it.

When the user taps on a notification in Facebook for Android, the class you configured in the App Dashboard settings will be invoked and the request information will pass into the intent data. The data will have the following information:
target_url=[URL]/?request_ids=[COMMA_SEPARATED_REQUESTIDs]
&ref=notif&fb_source=notification
&app_request_type=user_to_user
Process this info to direct users to the best experience based on the Request ID. In this sample, you'll display any data attached to the request or show a simple message.
Once you process the request, delete it from the Facebook server by making an HTTP DELETE request to the /[REQUEST_ID] Graph API endpoint. In this sample, you'll use the Facebook SDK API methods to delete the incoming request.
In this step, you'll handle an incoming request notification and show it to the user.
If you followed the Send Requests doc, you should have a MainFragment class where you'll process the incoming notification. The MainFragment fragment is hosted by MainActivity that matches the class you defined in your App Dashboard settings.
Open up your MainFragment class and add logic to examine if there's incoming intent data for an incoming request notification.
First, define a private string variable to hold any extracted request ID.
private String requestId;
Then, override the onActivityCreated() method to check for an incoming notification and save the info in the requestId member variable:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Check for an incoming notification. Save the info
Uri intentUri = getActivity().getIntent().getData();
if (intentUri != null) {
String requestIdParam = intentUri.getQueryParameter("request_ids");
if (requestIdParam != null) {
String array[] = requestIdParam.split(",");
requestId = array[0];
Log.i(TAG, "Request id: "+requestId);
}
}
}
Finally, modify the onSessionStateChange() method to show the request info if the user is authenticated:
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Check if the user is authenticated and
// an incoming notification needs handling
if (state.isOpened() && requestId != null) {
Toast.makeText(getActivity().getApplicationContext(), "Incoming request",
Toast.LENGTH_SHORT).show();
requestId = null;
}
if (state.isOpened()) {
sendRequestButton.setVisibility(View.VISIBLE);
} else if (state.isClosed()) {
sendRequestButton.setVisibility(View.INVISIBLE);
}
}
Note: To test the handling of incoming request notifications, you need the Facebook for Android app installed on your emulator or a real device.
Build and run the project on your test device to make sure it runs without errors. Tap the ''Log In'' button to log in with Facebook. Once authenticated, you should see the ''Send Request'' button created in the previous How To. Tap ''Send Request'' and send a request to a test user that you can log in with the Facebook for Android app. Once the request has been sent, tap ''Logout''.
Next, log into Facebook for Android as the test user and verify that you can see the notification you sent. Tap the notification and verify your app is launched and the login view is presented. Log in as the test user by clicking the login button. Verify you can see an incoming request.
Go back to Facebook for Android and verify the notification is still visible. Tap the notification and verify that you see an alert for the incoming request without having to log in.
In this step, you'll handle any additional data in the request. If your app sent additional data as shown in the Send Requests doc, you can get this data by making an HTTP GET request to the /[REQUEST_ID] Graph API endpoint. The Graph API data returned will be in the format:
{
"id": "493703870648580",
"application": {
"name": "Send Requests How To",
"id": "403223126407920"
},
"to": {
"name": "Chris Abe Colm",
"id": "100003086810435"
},
"from": {
"name": "Christine Abernathy",
"id": "1424840234"
},
"data": "{\"badge_of_awesomeness\":\"1\",\"social_karma\":\"5\"}",
"message": "Learn how to make your Android apps social",
"created_time": "2012-10-07T17:29:57+0000"
}
In this sample, instead of handling an incoming notification by displaying a simple message, you'll display the extra data.
Open up your MainFragment class and add a new private method called getRequestData() that will make the HTTP request to get the request data. It will customize the alert message based on the existence of extra data, no extra data or an error. Add the following code:
private void getRequestData(final String inRequestId) {
// Create a new request for an HTTP GET with the
// request ID as the Graph path.
Request request = new Request(Session.getActiveSession(),
inRequestId, null, HttpMethod.GET, new Request.Callback() {
@Override
public void onCompleted(Response response) {
// Process the returned response
GraphObject graphObject = response.getGraphObject();
FacebookRequestError error = response.getError();
// Default message
String message = "Incoming request";
if (graphObject != null) {
// Check if there is extra data
if (graphObject.getProperty("data") != null) {
try {
// Get the data, parse info to get the key/value info
JSONObject dataObject =
new JSONObject((String)graphObject.getProperty("data"));
// Get the value for the key - badge_of_awesomeness
String badge =
dataObject.getString("badge_of_awesomeness");
// Get the value for the key - social_karma
String karma =
dataObject.getString("social_karma");
// Get the sender's name
JSONObject fromObject =
(JSONObject) graphObject.getProperty("from");
String sender = fromObject.getString("name");
String title = sender+" sent you a gift";
// Create the text for the alert based on the sender
// and the data
message = title + "\n\n" +
"Badge: " + badge +
" Karma: " + karma;
} catch (JSONException e) {
message = "Error getting request info";
}
} else if (error != null) {
message = "Error getting request info";
}
}
Toast.makeText(getActivity().getApplicationContext(),
message,
Toast.LENGTH_LONG).show();
}
});
// Execute the request asynchronously.
Request.executeBatchAsync(request);
}
Next, modify the onSessionStateChange() method to call the new private method:
Toast.makeText(getActivity().getApplicationContext(), "Incoming request", Toast.LENGTH_SHORT).show();getRequestData(requestId);
Build and run the project to make sure it runs without errors. Open up the Facebook for Android app and tap on the notification that should be available from the previous step. Verify you see the incoming notification request and the confirmation shows the extra data:

Now that you've implemented the notification detection, delete the notification so that it's removed from the notification list.
In this step, you'll delete the request after you process it. You can delete it from Facebook by making an HTTP DELETE request to the /[REQUEST_ID] Graph API endpoint. You can make a similar call using the Facebook SDK.
In this sample, you'll start the deletion flow after showing the request notification or data.
Open your MainFragment class and define a new private method that deletes a given request ID:
private void deleteRequest(String inRequestId) {
// Create a new request for an HTTP delete with the
// request ID as the Graph path.
Request request = new Request(Session.getActiveSession(),
inRequestId, null, HttpMethod.DELETE, new Request.Callback() {
@Override
public void onCompleted(Response response) {
// Show a confirmation of the deletion
// when the API call completes successfully.
Toast.makeText(getActivity().getApplicationContext(), "Request deleted",
Toast.LENGTH_SHORT).show();
}
});
// Execute the request asynchronously.
Request.executeBatchAsync(request);
}
You may be asked to make a selection when importing newly required libraries for the Request class. If given a choice, select com.facebook.Request.
Next, call this method in the getRequestData() method after you display the request notification in your app:
....
Toast.makeText(getActivity().getApplicationContext(),
message,
Toast.LENGTH_LONG).show();
deleteRequest(inRequestId);
Only delete the request if you process the request and there are no errors. To do this, modify the code to only call the deleteRequest() method if there are no errors:
....
FacebookRequestError error = response.getError();
boolean processError = false;
....
} catch (JSONException e) {
processError = true;
....
} else if (error != null) {
processError = true;
....
Toast.makeText(getActivity().getApplicationContext(),
message,
Toast.LENGTH_LONG).show();
if (!processError) {
deleteRequest(inRequestId);
}
Build and run the project to make sure it runs without errors. Open up Facebook for Android and tap the notification that should be available from the previous step.
Verify that you see the incoming notification request and a confirmation that it was deleted. To verify that the notification was deleted, you may need to log out and log back in, as the notification may be cached. You can also check that it was deleted by going to the App Center Requests tab to make sure the notification isn't visible.