2011-12-01 79 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..."); 
      } 
    }