2013-03-13 78 views
0

我正在開發Android應用程序,我需要能夠定期獲取用戶有多少條未讀通知,並將其顯示在我的應用程序中。獲取Facebook通知API 3.0

我相信這在3.0 API中並不是直截了當的。

有人可以請解釋一下最好的方法嗎?

我使用這個代碼:

Request notificationsRequest = Request.newGraphPathRequest(
     Session.getActiveSession(), "/me/notifications", 
     new Request.Callback() { 

      @Override 
      public void onCompleted(Response response) { 
       GraphObject object = response.getGraphObject(); 
       if (object != null) { 
        notifications = object.getProperty("data") 
          .toString(); 
       } else { 
        notifications = "Notifications returns null"; 
       } 
      } 
     }); 

但它返回null

回答

1

您必須包含「include_read」和「true」。 你可以這樣做:

if (object != null) { 

    try { 

    JSONObject jsonObject = object.getInnerJSONObject().getJSONObject("summary"); 
    int notificationsUnread = jsonObject.getInt("unseen_count"); 

    Log.i("FB notificationsSummary", jsonObject + ""); 

    } catch (JSONException e) {    
     // TODO Auto-generated catch block 
     e.printStackTrace();       
    } 


    Bundle notificationParams = new Bundle(); 
    notificationParams.putString("include_read", "true"); 
    yourRequest.setParameters(notificationParams); 

    yourRequest.execute(); 
}