我在我的应用中使用了适用于Android的Facebook SDK。我似乎无法找到有关如何使用SDK代码获取通知的任何示例或文档。我有权限“manage_notifications”设置,我假设我需要使用.request()方法,但是graphPath参数使我失望。
有没有人举例说明如何使用面向Android的Facebook SDK获取Facebook通知?
答案 0 :(得分:3)
虽然其他答案很有帮助,但我所寻找的是Android Code的一个例子。我已经弄明白了,并在此发布。下面的代码获取登录/验证用户通知。
//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 :(得分:1)
默认情况下,/ USER_ID / notifications端点仅包含未读通知(即,如果Facebook.com顶部的第三颗宝石点亮并且内部有红色数字,则只会有返回值)
如果您还要包含用户已阅读的通知,您可以向/USER_ID/notifications?include_read=1
发出请求 - manage_notifications是对此的正确扩展权限
答案 2 :(得分: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);
}
答案 3 :(得分: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
请参阅此页面了解详情http://developers.facebook.com/docs/reference/fql/notification/
可以在实现BaseRequestListener的侦听器的onComplete()中接收此查询的结果。
答案 4 :(得分: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...");
}
}