5

I am trying to implement send data and accept that data in my Facebook android game app.I am following https://developers.facebook.com/docs/android/send-requests/#notifications tutorial for that. I am able to send the request however I am unable to accept the data at the recipient side .

My intentUri returning null every time so that's I am unable to get requestIds.

my code for getting the data at the recipient side:

intentUri = MainActivity.this.getIntent().getData();
if (intentUri != null) {
    String requestIdParam = intentUri.getQueryParameter("request_ids");
    if (requestIdParam != null) {
        String array[] = requestIdParam.split(",");
        requestId = array[0];
        Log.i("Request id: ", "Request id: " + requestId);

    }else{
        Log.i("Request id: ", "null" );

    }
} else {
    Log.i("intentUri", "null");     //**Always printing this part**
}

onSessionStateChange() method for calling requestData method

private void onSessionStateChange(Session session, SessionState state,Exception exception) {
    if (state.isOpened() && requestId != null) {
        getRequestData(requestId);
        requestId = null;
    }
}

I am sending request using following method :

private void sendRequestDialog() {
    Bundle params = new Bundle();
    params.putString("message",
            "Learn how to make your Android apps social");
    params.putString("data", "{\"badge_of_awesomeness\":\"1\","
            + "\"social_karma\":\"5\"}");
    WebDialog requestsDialog = (new WebDialog.RequestsDialogBuilder(
            MainActivity.this, Session.getActiveSession(), params))
            .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();
                        }
                    }
                }
            }).build();
    requestsDialog.show();
}

Code for accepting Request as below:

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(MainActivity.this, message,
                            Toast.LENGTH_LONG).show();
                    deleteRequest(inRequestId);
                }
            });
    // Execute the request asynchronously.
    Request.executeBatchAsync(request);
}

Note : I am just using simple activity , I am not using fragments

Kunu
  • 5,078
  • 6
  • 33
  • 61
  • Since the Facebook SDK changes so rapidly, I can only give you some directions. – Nick Jian Jun 25 '14 at 06:41
  • 1.First, Do you received any error when you send request?? Maybe the request is not send successfully. You have to check logcat carefully for suspicious warning/error. – Nick Jian Jun 25 '14 at 06:41
  • 2.Sometimes the setting in you facebook app console may make it visible/invisible on mobile/desktop. You may check fb app console setting. This part is so complicated and hard to debug, all you can do is try and error. – Nick Jian Jun 25 '14 at 06:41
  • @NickJian Thank you,Request is successfully sending without any error or warning and it also showing on the receivers notifications area on Facebook(web). However when I am accessing the data through `MainActivity.this.getIntent().getData();` code it showing null while printing in my logcat as I mentioned in the question. – Kunu Jun 25 '14 at 07:17

1 Answers1

0

The method getIntent() returns the intent that started the activity. You can try overriding the Activity method

protected void onNewIntent(Intent intent)

And call

setIntent(Intent intent)

from there.

You can also try requesting your data using:

Intent intent = MainActivity.this.getIntent();
Bundle bundle = intent.getExtras();
intentUri = (Uri)bundle.get(Intent.EXTRA_TEXT);

Aside from that, where and how are you calling intent.putExtra("request_ids", data) ?

Not sure if i've fully understood your problem,

anyways, i wish you good luck.

EDIT:

You may want to use Intent.EXTRA_STREAM instead of Intent.EXTRA_TEXT, depending on the way you store your data

  • Data I am sending is through Facebook native dialogs. Check the `sendRequestDialog()` of my question where I am sending data using params.putString("data","....."); So here no question of `intent.putExtra` – Kunu Jun 26 '14 at 11:31
  • Yes you are right, my fault. Have you checked the Facebook App Dashboard settings? Maybe you receive null data because you don't have the right permissions. Have you placed the code for getting Data (intentUri = MainActivity.this.getIntent().getData(); etc.) in the MainActivity onCreate method? It's surprising the fact that it doesn't work properly, considering it's almost copied and pasted from the Facebook SDK Docs examples. The only thing that comes to my mind right now it's the App Dashboard. Let me know how it goes. P.S: have you tried intentUri =(Uri)bundle.get(Intent.EXTRA_STREAM)? – stackbrains Jun 26 '14 at 12:48