Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook SDK - How To Query Facebook Notifications

I have the Facebook SDK for Android working in my app. I can't seem to find any examples or documentation on how to use the SDK code to get Notifications. I have the permission "manage_notifications" set and I am assuming that I need to use the .request() method, but the graphPath parameter eludes me.

Does anyone have an example of how to get the Facebook notifications using the Facebook SDK for Android?

like image 697
Camille Sévigny Avatar asked Dec 01 '11 14:12

Camille Sévigny


2 Answers

While the other answers are helpfull, what I was looking for was an example of the Android Code. I have figured it out though and have posted it here. The code below gets the logged in/authenticated users notifications.

//Initialze your Facebook object, etc.
Facebook _facebook = ...
...
Bundle bundle = new Bundle();
bundle.putString(Facebook.TOKEN, _accessToken);
String result = _facebook.request("me/notifications", bundle, "GET");

Then you will need to parse the string "result". It's in json format. Here is an example of what that will look like:

JSONObject jsonObjectResults = new JSONObject(result);
JSONArray jsonNotificationDataArray = jsonObjectResults.getJSONArray("data");
for (int i=0;i<jsonNotificationDataArray.length();i++)
{
    JSONObject jsonNotificationData = jsonNotificationDataArray.getJSONObject(i);
    if (_debug) Log.v("Title: " + jsonNotificationData.getString("title"));
}

I hope that other people find this useful.

like image 192
Camille Sévigny Avatar answered Oct 04 '22 05:10

Camille Sévigny


By default the /USER_ID/notifications endpoint only includes unread notifications (i.e there'll only be a return value if the third jewel on the top line of Facebook.com is lit up and has a red number inside it)

If you want to also include notifications the user has already read, you can make a request to /USER_ID/notifications?include_read=1 - manage_notifications is the correct extended permission for this

like image 42
Igy Avatar answered Oct 04 '22 05:10

Igy