0

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

我希望在我的应用程序中当用户收到一条消息,或者当用户收到一个喜欢或好友请求时得到通知。

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

4

1 回答 1

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.json 中的 JSONArray解析内容for loop。如果你只需要计数,你可以使用它JAFeeds.length()(参见Log.e上面的代码)

注意:由于您facebook-graph-api在 OP 中有标签,因此我建议使用 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 解决方案都是生产代码,可以正常工作。如果有特定的东西不起作用,请评论或更新 OP 的问题。

于 2013-03-13T10:23:29.880 回答