Android Facebook SDK-如何查询Facebook通知
我的应用程序中有用于Android的Facebook SDK。我似乎找不到任何关于如何使用SDK代码获取通知的示例或文档。我设置了权限“manage_notifications”,我假设我需要使用.request()方法,但graphPath参数无法使用Android Facebook SDK-如何查询Facebook通知,android,facebook,sdk,notifications,Android,Facebook,Sdk,Notifications,我的应用程序中有用于Android的Facebook SDK。我似乎找不到任何关于如何使用SDK代码获取通知的示例或文档。我设置了权限“manage_notifications”,我假设我需要使用.request()方法,但graphPath参数无法使用 有人举过如何使用Facebook Android SDK获取Facebook通知的例子吗?默认情况下,/USER\u ID/notifications端点仅包括未读的通知(即,只有当Facebook.com顶行的第三颗宝石亮起并且里面有一个红色
有人举过如何使用Facebook Android SDK获取Facebook通知的例子吗?默认情况下,/USER\u ID/notifications端点仅包括未读的通知(即,只有当Facebook.com顶行的第三颗宝石亮起并且里面有一个红色数字时,才会有返回值)
如果您还想包括用户已经阅读过的通知,您可以向
/user\u ID/notifications?include\u read=1
-manage\u notifications是此功能的正确扩展权限,而其他答案都是有帮助的,我要找的是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"));
}
JSONObject jsonObjectResults=新的JSONObject(结果);
JSONArray jsonNotificationDataArray=jsonObjectResults.getJSONArray(“数据”);
对于(int i=0;i您也可以使用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的侦听器的onComplete()中接收。您可以检查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);
}
这就是我收到通知的方式
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...");
}
}
final Session Session=Session.getActiveSession();
如果(session.isOpened()){
字符串aaa=新字符串();
aaa=“选择标题\文本,从收件人\ id=me()且\未读=1的通知中更新\时间”;
Bundle params=新Bundle();
参数putString(“q”,aaa);
新请求(会话“/fql”、参数、HttpMethod.GET、新请求.Callback(){
未完成公共无效(响应){
尝试
{
GraphObject go=response.getGraphObject();
JSONObject jso=go.getInnerJSONObject();
JSONArray arr=jso.getJSONArray(“数据”);
String spliting=arr.toString().replaceAll(“\\\\\\\\\{\\\\\}\\\\[\\\\]”,”);
String[]arrayresponse=spliting.split(“\\,”);
字符串s=“”;
for(int i=0;i13){
if(arrayresponse[i].子字符串(1,13).equals(“updated_time”))
s++=“*”+getDate(Long.valueOf(arrayresponse[i].子字符串(15,arrayresponse[i].length()))+“\n”;
其他的
s+=“”+arrayresponse[i]。子字符串(14,arrayresponse[i]。长度()-1)+“\n\n”;
}
}
text2.setVisibility(View.VISIBLE);
NotificationMessage.setVisibility(View.VISIBLE);
setMovementMethod(新的ScrollingMovementMethod());
NotificationMessage.setText;
阅读信箱(会话);
}捕获(可丢弃的t)
{
t、 printStackTrace();
}
}
}
).executeAsync();
}
否则{
//NotificationMessage.setVisibility(View.INVISIBLE);
Log.i(标记“注销…”);
}
}
此信息非常有用,谢谢。不过,我正在寻找有关request()方法的Android特定代码。请告诉我如何添加manage_通知权限。谢谢。我尝试过使用:facebook.authorize(这是新字符串[]{“email”,“publish_stream”,“manage_notifications”}…但它不起作用。在onCreate()或onComplete()中,在何处添加此代码@Vivekanand此代码应该添加到您想要查询Facebook通知json数据的任何地方。它可以在任何地方。@SalmanKhakwani-是的。您知道如何使用Facebook SDK v3.0来完成查询此数据吗?您可以使用Facebook SDK 3.0库中的小部件,并将此小部件添加到Layout.xml文件中。此小部件管理Faceboo添加此小部件后,只需向此小部件添加权限即可:`LoginButton authButton=(LoginButton)findViewById(R.id.authButton);authButton.setPublishPermissions(Arrays.asList(“阅读流”、“电子邮件”、“阅读好友列表”、“发布流”))我已经发布了一个答案,请检查下面的答案。这看起来像我要找的。有时间让我试试。