在我的Android应用程序上接收facebook更新

时间:2013-03-13 10:04:28

标签: android facebook facebook-graph-api

我想知道是否有人听说过使用facebook android sdk从facebook获取更新。

我希望在用户收到消息时通知我的应用程序,  或当用户收到喜欢或朋友的请求时。

你知道这样做的最小可能性吗? 我不想在我的应用程序中阅读消息或喜欢,我只是想得到通知:用户配置文件的新活动,以便我将增加一个变量。

1 个答案:

答案 0 :(得分:0)

请注意,虽然API允许您检查通知,但没有固有的方法可以自动获取它们。您必须找到一种定期获取新通知的方法,并在您的应用中显示它们。这超出了这个问题的范围,并且被排除在外。

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
}

现在使用JAFeeds中的for loop JSONArray解析内容。如果您只需要计数,可以使用JAFeeds.length()获取(请参阅上面代码中的Log.e

注意:由于OP中有facebook-graph-api标记,我建议使用Graph API的示例。但是,如果您需要使用FQL的解决方案,请告诉我。

<强>更新:

根据OP的评论,使用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
}

Graph API解决方案和FQL解决方案都是生产代码,可以正常工作。如果某些特定内容不起作用,请对问题进行评论或更新。