【问题标题】:Android Facebook SDK - How To Query Facebook NotificationsAndroid Facebook SDK - 如何查询 Facebook 通知
【发布时间】:2011-12-01 14:24:47
【问题描述】:

我的应用中有适用于 Android 的 Facebook SDK。我似乎找不到任何关于如何使用 SDK 代码获取通知的示例或文档。我设置了“manage_notifications”权限,并且假设我需要使用 .request() 方法,但我无法使用 graphPath 参数。

有没有人举例说明如何使用 Android 版 Facebook SDK 获取 Facebook 通知?

【问题讨论】:

    标签: android facebook sdk notifications


    【解决方案1】:

    虽然其他答案很有帮助,但我正在寻找的是 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"));
    }
    

    我希望其他人觉得这很有用。

    【讨论】:

    • 我在哪里添加此代码.. 在 onCreate() 或 onComplete() 中
    • @Vivekanand 此代码应添加到您要查询 Facebook 通知 json 数据的任何位置。它可以在任何地方。
    • @SalmanKhakwani - 是的。你知道如何使用 Facebook SDK v3.0 来完成对这些数据的查询吗?
    • 您可以使用 Facebook SDK 3.0 库中的 小部件并将此小部件添加到您的 Layout.xml 文件中。这个小部件管理 Facebook 会话本身,所以你不必担心。添加此小部件后,只需向此小部件添加权限:`LoginButton authButton = (LoginButton)findViewById(R.id.authButton); authButton.setPublishPermissions(Arrays.asList("read_stream", "email", "read_friendlists", "publish_stream"));`
    • 我已经发布了答案,请查看下面的答案。
    【解决方案2】:

    默认情况下,/USER_ID/notifications 端点只包含未读通知(即,只有当 Facebook.com 顶行的第三颗宝石亮起并且里面有一个红色数字时才会有返回值)

    如果您还想包含用户已经阅读的通知,您可以向/USER_ID/notifications?include_read=1 提出请求 - manage_notifications 是正确的扩展权限

    【讨论】:

    • 这个信息很有帮助,谢谢。不过,我一直在寻找有关 request() 方法的 Android 特定代码。
    • 你能告诉我如何添加manage_notification权限吗?谢谢。我试过用这个:facebook.authorize(this, new String[] { "email", "publish_stream" , "manage_notifications" }。 ..但它不起作用。
    【解决方案3】:

    您可以检查 Facebook SDK 3.0 的 Session 对象以确保 Session 已打开。 之后,您可以借助以下代码获取 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); 
         }
    

    【讨论】:

    • 这看起来像我要找的。有空的时候让我试试看。
    【解决方案4】:

    您还可以使用 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 
    



    详情请参考本页http://developers.facebook.com/docs/reference/fql/notification/

    可以在实现 BaseRequestListener 的侦听器的 onComplete() 中接收此查询的结果。

    【讨论】:

      【解决方案5】:

      这就是我收到通知的方式

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-11
        • 2012-09-18
        相关资源
        最近更新 更多