Search code examples
javaandroidfacebookfacebook-android-sdkfacebook-app-requests

request id is null in Handle the Incoming Notification in facebook android sdk


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


Solution

  • 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