2011-03-22 62 views

回答

3

這是我如何得到使用Facebook SDK爲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")); 
} 

我希望你覺得這個很有用。

+1

這個代碼需要manage_notification permission.may請你幫me.how做我分配此權限。謝謝 – 2014-06-04 13:00:04

0

這是一個老問題,但我會後我如何從Facebook的通知,如果有人發現了它有用

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..."); 
      }