android:如何在我的应用程序中获取Facebook通知通知数据

时间:2015-02-03 07:08:45

标签: android facebook

我已创建Android应用程序以显示来自facebook通知的警报。 我正在使用facebook SDK。任何人都可以帮我做同样的事情。

1 个答案:

答案 0 :(得分:2)

一种方法是使用图形API request来获取用户的所有通知。我使用AsyncTask实现了它。

 private class GetNotification extends
        AsyncTask<String, String, List<Notification>> {
    boolean isTaskDone = true;

    public boolean isTaskDone() {
        return isTaskDone;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    @Override
    protected List<Notification> doInBackground(String... params) {
        // TODO Auto-generated method stub
        final ArrayList<Notification> temp_page_list = new ArrayList<Notification>();
        Request.GraphUserCallback graphUserCallback;
        graphUserCallback = new Request.GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {
                Log.d("pagenot", "in completed");
                JSONObject jsonObject = null;
                if (user != null) {
                    jsonObject = user.getInnerJSONObject();
                    try {
                        JSONArray temPageList = jsonObject
                                .getJSONArray("data");
                        Log.d("pagenot", "length - " + temPageList.length());
                        for (int i = 0; i < temPageList.length(); i++) {
                            JSONObject JOAlbums = temPageList
                                    .getJSONObject(i);
                            Notification item = new Notification();

                            if (JOAlbums.has("id")) {
                                item.setId(JOAlbums.getString("id"));
                            } else {
                                item.setId(null);
                            }

                            if (JOAlbums.has("from")) {
                                JSONObject from = JOAlbums
                                        .getJSONObject("from");
                                if (from.has("name"))
                                    item.set_fron_name(from
                                            .getString("name"));
                                else
                                    item.set_fron_name("");
                                if (from.has("id"))
                                    item.set_from_id(from.getString("id"));
                                else
                                    item.set_from_id("");
                            }

                            if (JOAlbums.has("link")) {
                                item.setLink(JOAlbums.getString("link"));
                            } else {
                                item.setLink("");
                            }

                            if (JOAlbums.has("created_time")) {
                                item.setCreatedd_time(JOAlbums
                                        .getString("created_time"));
                            } else {
                                item.setCreatedd_time("");
                            }

                            if (JOAlbums.has("updated_time")) {
                                item.setUpdated_time(JOAlbums
                                        .getString("updated_time"));
                            } else {
                                item.setUpdated_time("");
                            }

                            if (JOAlbums.has("title")) {
                                item.setTitle(JOAlbums.getString("title"));
                            } else {
                                item.setTitle("");
                            }

                            if (JOAlbums.has("application")) {
                                JSONObject application = JOAlbums
                                        .getJSONObject("application");
                                if (application.has("name"))
                                    item.setName(application
                                            .getString("name"));
                                else
                                    item.setName("");
                            }

                            if (JOAlbums.has("unread"))
                                item.setUnread(JOAlbums.getInt("unread"));
                            else
                                item.setUnread(0);

                            temp_page_list.add(item);
                        }
                        isTaskDone = false;
                        return;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.d("pagenot", "user is null");
                }
            }
        };

        // assign callback to final instance variable in inner class
        final Request.GraphUserCallback finalCallback = graphUserCallback;
        Request.Callback wrapperCallback = new Request.Callback() {
            @Override
            public void onCompleted(Response response) {
                finalCallback.onCompleted(
                        response.getGraphObjectAs(GraphUser.class),
                        response);
            }
        };
        Request request = new Request(Session.getActiveSession(),
                "/me/notifications", null, HttpMethod.GET, wrapperCallback);

        Request.executeAndWait(request);
        return temp_page_list;
    }

    @Override
    protected void onPostExecute(List<Notification> result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }
}

和Notification类代码将是:

public class Notification {
private String id;
private String _from_id;
private String _fron_name;
private String createdd_time;
private String updated_time;
private String title;
private String link;
private String name;
private int unread;

public Notification(){}
public String getId() {
    return id;
}

public String get_from_id() {
    return _from_id;
}
public void set_from_id(String _from_id) {
    this._from_id = _from_id;
}
public String get_fron_name() {
    return _fron_name;
}
public void set_fron_name(String _fron_name) {
    this._fron_name = _fron_name;
}
public String getCreatedd_time() {
    return createdd_time;
}
public void setCreatedd_time(String createdd_time) {
    this.createdd_time = createdd_time;
}
public String getUpdated_time() {
    return updated_time;
}
public void setUpdated_time(String updated_time) {
    this.updated_time = updated_time;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getLink() {
    return link;
}
public void setLink(String link) {
    this.link = link;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getUnread() {
    return unread;
}
public void setUnread(int unread) {
    this.unread = unread;
}
public void setId(String string) {
    // TODO Auto-generated method stub
    this.id = string;
}

}

我希望这会有所帮助。