Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get Facebook messages in Android

    • 0
    • 3
    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 443
    Comment on it

    Note- Facebook provides 25 latest message conversation.

    To get Facebook chat messages in Android, following steps are used:

    1. Define the 'read_mailbox' property as below:

      private static final List PERMISSIONS = Arrays.asList("read_mailbox");

    2. Write the following code to get messages from Facebook:

    Session.openActiveSession(createNewTextShareFromFacebookActivity, true, new Session.StatusCallback() {
    
                // callback when session changes state
                @Override
                public void call(final Session session, SessionState state, Exception exception) {
                    if (session.isOpened()) {
                        List permissions = session.getPermissions();        
                        // If permission is not already provided then it will provide the 'read_mailbox' permission to read inbox messages from facebokk
                        if (!isSubsetOf(PERMISSIONS, permissions)) {
                            Session.NewPermissionsRequest newPermissionsRequest = new Session
                                    .NewPermissionsRequest(createNewTextShareFromFacebookActivity, PERMISSIONS);
                            session.requestNewReadPermissions(newPermissionsRequest);
                            return;
                        }
                        Request req = new Request(session, "/me/inbox",null, HttpMethod.GET,
                                new Callback() {
    
                            @Override
                            public void onCompleted(Response response) {
    
                                if(response.getGraphObject() != null)
                                {
                                    JSONObject graphResponse = response
                                            .getGraphObject()
                                            .getInnerJSONObject();
                                    // Here we can print the graphResponse object to see the messages given by facebook
                                }
                            }
                        });
                        Request.executeBatchAsync(req);
                    }
                }
            });
        

    Check permission is provided or not:

    // Check the permission is already provided or not
        private boolean isSubsetOf(Collection subset, Collection superset) {
            for (String string : subset) {
                if (!superset.contains(string)) {
                    return false;
                }
            }
            return true;
        }

    Hope this will help you. Good luck :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: