0

I want to know if someone heard about getting updates from facebook with facebook android sdk.

I want in my application to be notified when the user received a message, or when user received a like or a friend request.

Do you know the smallest possiblity to do this? I dont want to read messages or likes in my app, i just want to be notified: New activity on user profile, so that i will increment a variable.

Dany's
  • 943
  • 1
  • 7
  • 17

1 Answers1

0

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.

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • @dany's: let me get to the office for the FQL code. But what didn't work? This is code used in production and works perfect. – Siddharth Lele Mar 14 '13 at 03:09
  • maybe i dont know to initialize facebook session...can you please provide something complete?...i mean i think i need permissions, i need to set an ACCES TOCKEN and an APP ID, what is the order to do all that...Thanks a lot, You would be a life savior if you can tell that – Dany's Mar 14 '13 at 11:02
  • @Dany's: You don't need an App ID in every query. As for the _permission_, you need your app to get the [Manage Notifications permission](https://developers.facebook.com/docs/reference/login/extended-permissions/). – Siddharth Lele Mar 14 '13 at 11:05
  • @Dany's: And you won't need to specifiy the Access Token if you use FQL. This query is used with the Facebook SDK v.2.X. If you are using the newer 3.x SDK, I would recommend using the solution from this post: http://stackoverflow.com/a/13163261/450534. It shows how to use the FQL query with the latest SDK. It does not address your specific requirement. – Siddharth Lele Mar 14 '13 at 11:08
  • @Dany's: No problem. Take your time. And do let me know if you need assistance. – Siddharth Lele Mar 14 '13 at 12:07
  • seriously...this code not working ...i put it there and i receive code 200 but object null,...can you provide a complete code?..with session initialization and permissions and all it needs?...i have putted anage_notifications...but in new sdk 3.0 says the manage_notifications is not a NewReadREquest – Dany's Mar 14 '13 at 19:23
  • @Dany's: Try the 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` in the FQL Query tab here: https://developers.facebook.com/tools/explorer/. I do not work on the SDK v3.x and have no idea how to work on it because I never tried. – Siddharth Lele Mar 15 '13 at 03:34