Please note that although the API allows you to check for Notifications, there is no inherent method to get them automatically. You will have to figure out a way to fetch new Notifications periodically and display them in your app. That is beyond the context of this question and has been left out.
try {
Bundle bun = new Bundle();
bun.putString(Facebook.TOKEN, Utility.mFacebook.getAccessToken());
bun.putString("include_read", "true"); // CHANGE "true" to "false" if you need just the unread / unseen notifications
String result = Utility.mFacebook.request("/me/notifications", bun);
JSONObject JORoot = new JSONObject(result);
JSONArray JAFeeds = JORoot.getJSONArray("data");
Log.e("TEST LENGTH", String.valueOf(JAFeeds.length()));
} catch (Exception e) {
// TODO: handle exception
}
Now parse the the content using the JAFeeds
JSONArray in a for loop
. If you need just the count, you can get it using JAFeeds.length()
(see the Log.e
in the code above)
Note: Since you have the facebook-graph-api
tag in the OP, I am suggesting an example using the Graph API. However, if you need a solution using FQL, let me know.
UPDATED:
As per the comment by the OP, adding an alternative using FQL
try {
String query = "SELECT notification_id, recipient_id, object_id, object_type, sender_id, title_text, body_text, href, created_time FROM notification WHERE recipient_id=me() AND is_unread = 1 AND is_hidden = 0"
Bundle newNotifications = new Bundle();
newNotifications.putString("method", "fql.query");
newNotifications.putString("query", queryStatus);
String responseNewNotifications = Utility.mFacebook.request(newNotifications);
JSONArray JANotifications = new JSONArray(responseNewNotifications);
Log.e("TEST LENGTH", String.valueOf(JANotifications.length()));
} catch (Exception e) {
// TODO: handle exception
}
Both the Graph API solution as well as the FQL solution are production codes and work as they should. If there is something specific that does not work, please comment or update the OP with the issue.