使用facebook sdk从android接收来自Facebook的通知?

时间:2014-06-04 06:27:09

标签: android facebook facebook-graph-api notifications

我正在创建一个Android应用程序,我希望获得登录用户的Facebook通知。我尝试了一个代码。我的问题是

1.如何设置MANAGE_NOTIFICATIONS权限? 2.不确定我的代码是否正确

以下是我获取通知的代码。 任何帮助将不胜感激......

public void getNotification() 
    {
        mAsyncRunner.request("me/notifications", new RequestListener() {
            @Override
            public void onComplete(String response, Object state) {
                Log.d("Notifications", response);
                String json = response;
                try {
                    // Facebook Profile JSON data
                    JSONObject notifications = new JSONObject(json);

                    // getting name of the user
                     name = notifications.getString("name");

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "Notifications..."+name, Toast.LENGTH_LONG).show();
                        }

                    });


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onIOException(IOException e, Object state) {
            }

            @Override
            public void onFileNotFoundException(FileNotFoundException e,
                    Object state) {
            }

            @Override
            public void onMalformedURLException(MalformedURLException e,
                    Object state) {
            }

            @Override
            public void onFacebookError(FacebookError e, Object state) {
            }
        });

1 个答案:

答案 0 :(得分:0)

我发现了另一种方式。如果有人发现它有用,我会发布它

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
    InboxMessage.setVisibility(View.VISIBLE);

    new Request(session,"/me/notifications", null,HttpMethod.GET,new Request.Callback() {

                public void onCompleted(Response response) {
                    GraphObject object = response.getGraphObject();
                        if (object != null) {
                            InboxMessage.setText(object.getProperty("data").toString());
                        } else {
                            InboxMessage.setText("Notifications returns null");
                        }
                }
            }
        ).executeAsync();
} else {
    InboxMessage.setVisibility(View.INVISIBLE);
    Log.i(TAG, "Logged out...");
}
}