Android Facebook SDK - How To Query Facebook Notifications

本秂侑毒 提交于 2019-12-05 05:55:29

While the other answers are helpfull, what I was looking for was an example of the Android Code. I have figured it out though and have posted it here. The code below gets the logged in/authenticated users notifications.

//Initialze your Facebook object, etc.
Facebook _facebook = ...
...
Bundle bundle = new Bundle();
bundle.putString(Facebook.TOKEN, _accessToken);
String result = _facebook.request("me/notifications", bundle, "GET");

Then you will need to parse the string "result". It's in json format. Here is an example of what that will look like:

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

I hope that other people find this useful.

By default the /USER_ID/notifications endpoint only includes unread notifications (i.e there'll only be a return value if the third jewel on the top line of Facebook.com is lit up and has a red number inside it)

If you want to also include notifications the user has already read, you can make a request to /USER_ID/notifications?include_read=1 - manage_notifications is the correct extended permission for this

You can check the Session Object of Facebook SDK 3.0 to ensure the Session is opened. After that you can get the JSON data with the help of following code:

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

You can also use a FQL query. The format of the query will be

SELECT notification_id, sender_id, title_html, body_html, href
FROM notification
WHERE recipient_id=userid
AND is_unread = 1
AND is_hidden = 0 



Please refer to this page for details http://developers.facebook.com/docs/reference/fql/notification/

The results of this query can be received in onComplete() of a listener which implements BaseRequestListener.

This is how I get notifications

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...");
             }
     }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!