我目前正在网站上开发一个Facebook应用程序,需要在不使用用户界面对话框的情况下向应用程序的用户发送通知。在阅读了一些博客后,我得出结论,该选项仅在PHP API的示例中可用。我只能找到这个例子:
http://developers.facebook.com/docs/channels/
是否有JavaScript API来执行此操作?
经过一些更多的阅读后,我发现FB.api可以处理图形对象apis以及其他要弃用的api,我得到了以下工作:
FB.api('/1175241653/apprequests', 'post',
{ message: "This is a Good Request!!" },
function (response) {
if (!response || response.error) {
alert('Error occured , Request Failed :(( ');
} else {
alert('Request is sent successfully');
}
});
但是,如果登录用户的ID不是该ID,则该ID号1175241653
不起作用。
因此,这需要与Facebook用来检索登录到应用程序的任何人的ID相同的功能。有没有办法做到这一点?
答案 0 :(得分:1)
现在,我得到了一切工作,我想与那些可能处理的人分享:))
让我们说第一个应用程序请求从您的应用程序到您的应用程序中的任何Facebook注册用户将是这样的:
var data =
{
message: "Hey there, something good happened over here !",
access_token: "AAADdf39DLxgBANEwZA9ZCfZCSbtdfcZBtstWMMsW5JiZBjVW2Ucx234sedhHSZCm8aEABzvhWPBNWi1bTKwZBq0EcgZD"
}
FB.api('/68751034/apprequests',
'post',
data,
function (response) {
console.log(response);
if (!response || response.error) {
} else {
}
});
应提供 access_token
以验证从应用程序到注册用户的请求。
如果您不了解访问令牌,可以在Facebook网站上阅读:
http://developers.facebook.com/docs/authentication/
此外,如果您想在一个请求调用中向一组用户发送批处理请求,那么Facebook网站上也会有一个支持页面:
http://developers.facebook.com/docs/reference/api/batch/
以下是我的意思的样本:
var _batch = [];
for (var i = 0; i < _socialids.length; i++) {
_batch.push({
method: 'post',
relative_url: _socialids[i] + '/apprequests/?access_token=' + _accessTokens[i],
body: "message= This is a request sent to many users" });
}
if (_batch.length > 0) {
FB.api('/', 'POST', { batch: _batch }, function (res) {
// Do whatever when the batch request is sent
});
}