2011-12-01 77 views
6

我有Android SDK的Facebook SDK工作在我的應用程序。我似乎無法找到任何有關如何使用SDK代碼獲取通知的示例或文檔。我有權限「manage_notifications」設置,我假設我需要使用.request()方法,但graphPath參數沒有我。Android Facebook SDK - 如何查詢Facebook通知

有沒有人有過如何使用Facebook SDK for Android獲取Facebook通知的例子?

回答

3

而其他的答案是有幫助的,我一直在尋找的是一個正確的擴展權限Android代碼示例。我已經弄明白了,並已將其發佈在此處。下面的代碼獲取登錄/認證的用戶通知。

//Initialze your Facebook object, etc. 
Facebook _facebook = ... 
... 
Bundle bundle = new Bundle(); 
bundle.putString(Facebook.TOKEN, _accessToken); 
String result = _facebook.request("me/notifications", bundle, "GET"); 

然後你需要解析字符串「result」。它採用json格式。下面是一個例子:

JSONObject jsonObjectResults = new JSONObject(result); 
JSONArray jsonNotificationDataArray = jsonObjectResults.getJSONArray("data"); 
for (int i=0;i<jsonNotificationDataArray.length();i++) 
{ 
    JSONObject jsonNotificationData = jsonNotificationDataArray.getJSONObject(i); 
    if (_debug) Log.v("Title: " + jsonNotificationData.getString("title")); 
} 

我希望其他人覺得這很有用。

+0

我在哪裏添加此代碼..在onCreate()或onComplete() – Vivekanand

+0

@Vivekanand此代碼應該被添加到任何你想查詢Facebook通知json數據的位置,它可以在任何地方。 –

+0

_facebook對象折舊:) –

1

默認情況下,/ USER_ID /通知端點僅包含未讀通知(例如,如果Facebook.com頂行的第三顆珠寶點亮並且裏面有紅色數字,則只會有返回值)

如果您還想包括用戶已經讀的通知,就可以使/USER_ID/notifications?include_read=1一個請求 - manage_notifications是這個

+0

此信息非常有幫助,謝謝。然而,我正在尋找有關request()方法的Android特定代碼。 –

+0

可以請你告訴我如何添加manage_notification使用此permission.Thankyou.i嘗試:facebook.authorize(這一點, \t \t \t \t \t新的String [] { 「電子郵件」, 「publish_stream」, 「manage_notifications」} ..但它不工作 –

0

您也可以使用FQL查詢。查詢的格式爲

SELECT notification_id, sender_id, title_html, body_html, href 
FROM notification 
WHERE recipient_id=userid 
AND is_unread = 1 
AND is_hidden = 0 



請參閱本頁面它實現BaseRequestListener偵聽器的細節http://developers.facebook.com/docs/reference/fql/notification/

此查詢的結果可以的onComplete接收()。

1

您可以檢查Facebook SDK 3.0的會話對象以確保會話已打開。 之後,你可以用下面的代碼的幫助下得到了JSON數據:

Session session = Session.getActiveSession(); 
    if (session.isOpened()) 
    { 
     //access_token = session.getAccessToken(); 
     Request graphRequest = Request.newGraphPathRequest(session, "me/home", new  
     Request.Callback() 
    { 
    public void onCompleted(Response response) 
      { 
       //Create the GraphObject from the response 
       GraphObject responseGraphObject = response.getGraphObject(); 

       //Create the JSON object 
       JSONObject json = responseGraphObject.getInnerJSONObject(); 
       Log.i("JSON", json.toString()); 
       try 
       { 
        YOUR_JSON_ARRAY= json.getJSONArray("data"); 
       } 
       catch (JSONException e) 
       { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
    Request.executeBatchAsync(graphRequest); 
    } 
+1

這看起來像我在找什麼。當我有空時,讓我試試看。 –

0

這是我得到的通知

final Session session =Session.getActiveSession(); 
      if(session.isOpened()){ 
       String aaa=new String(); 
       aaa="SELECT title_text,updated_time FROM notification WHERE recipient_id=me() AND is_unread=1"; 
       Bundle params = new Bundle(); 
       params.putString("q", aaa); 
        new Request(session,"/fql",params,HttpMethod.GET,new Request.Callback() { 
           public void onCompleted(Response response) { 
            try 
            { 
             GraphObject go = response.getGraphObject(); 
             JSONObject jso = go.getInnerJSONObject(); 
             JSONArray arr = jso.getJSONArray("data"); 
             String splitting=arr.toString().replaceAll("\\\\|\\{|\\}|\\[|\\]", ""); 
             String[] arrayresponse=splitting.split("\\,"); 
             String s = ""; 
             for (int i = 0; i < arrayresponse.length; i++) { 
              if (arrayresponse[i].length()>13){ 
               if (arrayresponse[i].substring(1,13).equals("updated_time")) 
                s+="* "+getDate(Long.valueOf(arrayresponse[i].substring(15,arrayresponse[i].length())))+"\n"; 
               else 
                s+=" "+arrayresponse[i].substring(14,arrayresponse[i].length()-1)+"\n\n"; 

              } 
             } 
             text2.setVisibility(View.VISIBLE); 
             NotificationMessage.setVisibility(View.VISIBLE); 
             NotificationMessage.setMovementMethod(new ScrollingMovementMethod()); 
             NotificationMessage.setText(s); 
             readMailBox(session); 

            }catch (Throwable t) 
            { 
             t.printStackTrace(); 
            } 


           } 
          } 
        ).executeAsync(); 
      } 
      else{ 
    //   NotificationMessage.setVisibility(View.INVISIBLE); 
       Log.i(TAG, "Logged out..."); 
      } 
    }