2011-03-22 52 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..."); 
      }