Note- Facebook provides 25 latest message conversation.
To get Facebook chat messages in Android, following steps are used:
Define the 'read_mailbox' property as below:
private static final List PERMISSIONS = Arrays.asList("read_mailbox");
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)