使用Facebook Api从Facebook获取通知

时间:2011-03-22 05:46:39

标签: android facebook

我想通知facebook ... 我正在使用this facebook Api ..

我希望在我的申请中提供此通知。 任何人都知道如何获得通知..

Image

2 个答案:

答案 0 :(得分:3)

这是我使用Facebook SDK for 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 :(得分: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...");
             }