3

I'm trying to handle incoming Notification in facebook.

ref : http://developers.facebook.com/docs/howtos/androidsdk/3.0/app-link-requests/

Code :

public class MainFragment extends Fragment{

private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;
private String requestId;
private TextView username;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d(TAG, "MainFragment.onCreate()");
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    Log.d(TAG, "MainFragment.onCreateView()");
    View view = inflater.inflate(R.layout.login,container, false);

    LoginButton authButton = (LoginButton) view.findViewById(R.id.loginButton);
    authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));
    authButton.setFragment(this);

    username = (TextView) view.findViewById(R.id.textusename);

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Log.d(TAG, "MainFragment.onActivityCreated()");

    // Check for an incoming notification. Save the info
    // Parse any incoming notifications and save

    Uri intentUri = getActivity().getIntent().getData();
    if (intentUri != null) {
        String requestIdParam = intentUri.getQueryParameter("request_ids");
        if (requestIdParam != null) {
            String array[] = requestIdParam.split(",");
            requestId = array[0];
        }
    }


}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {

    // Check if the user is authenticated and
    // an incoming notification needs handling 
    Log.d(TAG,"MainFragment.onSessionStateChange()");

    Log.d(TAG, "Request id : "+requestId);

    if (state.isOpened() && requestId != null) {
        Log.d("request id", requestId);

        getRequestData(requestId);

        Toast.makeText(getActivity().getApplicationContext(), "Incoming request", Toast.LENGTH_SHORT).show();
        requestId = null;
    }

    if (state.isOpened())
    {
        Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);


        Request.executeMeRequestAsync(Session.getActiveSession(), new GraphUserCallback() {

            @Override
            public void onCompleted(GraphUser user, Response response) {
                if (user != null) {
                    username.setText(user.getName());
                }                   
            }
        });

    }



}

private Session.StatusCallback callback = new Session.StatusCallback() {

    @Override
    public void call(Session session, SessionState state, Exception exception) {

        onSessionStateChange(session, state, exception);
    }
};

private void getRequestData(String requestid)
{
    Request request = new Request(Session.getActiveSession(), requestid, null, HttpMethod.GET, new Request.Callback() {

        @Override
        public void onCompleted(Response response) {

            // Process the returned response
            GraphObject graphObject = response.getGraphObject();
            FacebookRequestError error = response.getError();

            Log.d(TAG, response.toString());

            // 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(graphObject.getProperty("data").toString());

                        // 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 (Exception e) {
                        e.printStackTrace();
                    }
                }
                Toast.makeText(getActivity().getApplicationContext(),message,Toast.LENGTH_LONG).show();
            }

        }
    });

    Log.d(TAG, "Request : "+request.toString());

    // Execute the request asynchronously.
    Request.executeBatchAsync(request);

}
}

Logcat

03-02 17:40:20.443: D/MainFragment(947): MainFragment.onCreate()
03-02 17:40:20.463: D/MainFragment(947): MainFragment.onCreateView()
03-02 17:40:20.753: D/MainFragment(947): MainFragment.onActivityCreated()
03-02 17:40:28.623: D/MainFragment(947): MainFragment.onSessionStateChange()
03-02 17:40:28.623: D/MainFragment(947): Request id : null
03-02 17:40:30.673: D/MainFragment(947): MainFragment.onSessionStateChange()
03-02 17:40:30.673: D/MainFragment(947): Request id : null
03-02 17:40:30.823: D/MainFragment(947): MainFragment.onSessionStateChange()
03-02 17:40:30.833: D/MainFragment(947): Request id : null

Now my problem is that when I click on Notification using Facebook android app my app is start but I can not get the request id for that notification.

App Dashboard Configuration enter image description here

Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80

2 Answers2

1

I suppose you didn't forget the requestId attribute:

private String requestId;

Could you also check the values of intentUri, requestIdParam and array[0] in the onActivityCreated method?

@Override
public void onActivityCreated(Bundle savedInstanceState) { // <--- in THIS method
    super.onActivityCreated(savedInstanceState);
    Log.d(TAG, "MainFragment.onActivityCreated()");

    // Check for an incoming notification. Save the info
    Uri intentUri = getActivity().getIntent().getData();
    Log.d(TAG, "intentUri: " + intentUri.toString()); // <--- HERE
    if (intentUri != null) {
        String requestIdParam = intentUri.getQueryParameter("request_ids");
        Log.d(TAG, "requestIdParam: " + requestIdParam); // <--- HERE
        if (requestIdParam != null) {
            String array[] = requestIdParam.split(",");
            Log.d(TAG, "requestId: " + array[0]); // <--- HERE
            requestId = array[0];
        }
    }
}

Please update your post to show us the whole class.

Edit:

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

However, you don't get the request_ids parameter. In my opinion, there is something wrong with the following configuration: App Dashboard settings

What did you put in these fields? Does it tally with your actual app arborescence?

Edit 2:

Class Name: This is the class name of the main activity you want Facebook to launch.

What is there in your Login.java?

It should be com.pricus.expensemanagement.MainActivity.

Make sure you've correctly followed this first tutorial about login, and particularly this step:

First, make the activity subclass FragmentActivity instead of Activity:

public class MainActivity extends FragmentActivity {
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
  • No, I'm asking you to print the values of the above-named variables. Post edited with `HERE` statements. Do that and answer with the results of the three `log.d`. – Stéphane Bruckert Mar 02 '13 at 11:43
  • in logcat I'm getting null – Kirit Vaghela Mar 02 '13 at 11:52
  • Read again my last comment and answer the request. Can't believe you added +50 to your post and you don't even bother answering seriously. I want 3 (THREE) values. – Stéphane Bruckert Mar 02 '13 at 12:02
  • Sorry.I have add the logcat output – Kirit Vaghela Mar 02 '13 at 12:14
  • 2
    Seriously? This is still not what I want. I know since the beginning that requestId is NULL in `onSessionStateChange`. Your logcat output states what we already know. But tell me, do you read what I write? I'm asking you to add 3 `logs.d` in the `onActivityCreated` method. After three comments, you still didn't understand and we didn't progress at all. (Ok, I'll give you a hint because I'm nice: you only have to copy/paste my `onActivityCreated` method into your code). Then update again the logcat output. – Stéphane Bruckert Mar 02 '13 at 12:29
  • Logcat : D/MainFragment(487): intentUri: http://apps.facebook.com/apps/application.php?id=504782202896276 – Kirit Vaghela Mar 02 '13 at 13:05
  • Their is no "request_ids" parameter in intentUri= "http://apps.facebook.com/apps/application.php?id=504782202896276". so how it can print the value of "requestIdParam" parameter – Kirit Vaghela Mar 07 '13 at 09:16
  • Can you check that you indeed received the notification by connecting to your test user on Facebook (not on your app)? We need to know whether you successfully realized the [first task](https://developers.facebook.com/docs/howtos/androidsdk/3.0/send-requests/). – Stéphane Bruckert Mar 07 '13 at 09:50
  • Yes.I received the notification in facebook. when i click on notification my app is start – Kirit Vaghela Mar 07 '13 at 10:43
  • Did you configure the "[App Dashboard Configuration](https://developers.facebook.com/docs/howtos/androidsdk/3.0/app-link-requests/#prerequisities)" correctly? Could you show us? – Stéphane Bruckert Mar 07 '13 at 11:10
  • Yes. I can do anything with my current app Dashboard configuration. like send request,post a story, get user data using fql query etc. and what you want to see in app dashboard configuration – Kirit Vaghela Mar 07 '13 at 11:15
  • I've edited my answer. What did you put in the `Package Name`, `Class Name`, `Key Hash`, `Facebook Login` and `Deep Linking` fields? – Stéphane Bruckert Mar 07 '13 at 11:24
1

This is a problem I have spent whole day on it. Thanks to Boban

This is Facebook bug. Just add Mobile Web in App Dashboard and you will get request_ids

Android app : Handling app request posted on Facebook

Community
  • 1
  • 1
Nick Jian
  • 457
  • 4
  • 12
  • Hello Nick, I am having same kinda problem and you can check my question [http://stackoverflow.com/questions/24324963/unable-to-get-facebook-incoming-requests](http://stackoverflow.com/questions/24324963/unable-to-get-facebook-incoming-requests) also.If possible then please help me with this. – Kunu Jun 24 '14 at 06:07