简体   繁体   中英

sending notifications to user's friend in facebook using js sdk

I am trying to send notification to user's friend using js sdk on facebook canvas app but I get this in console

POST https://graph.facebook.com/16542203957691/notifications? 400 (OK) jquery.min.js:140
c.extend.ajax jquery.min.js:140
c.extend.post jquery.min.js:133
makePost ec2-34-41-111-91.ap-southwest-8.compute.amazonaws.com/:69
onclick

I am calling makePost function passing it the friend's profile Id as the argument, this is what I am trying

function makePost(personid){
  var accesstoken =   FB.getAuthResponse()['accessToken'];
  var address = "https://graph.facebook.com/" + personid + "/notifications?";
  var tempdata = {};
  tempdata['access_token'] = accesstoken;
  tempdata['href'] = "";
  tempdata['template'] = "You have earned 5 credits, Click here to redeem";
  jQuery.post(address, {json: JSON.stringify(tempdata)}, function(data){
      console.log(data);
  });
}

the person is not receiving the notification.

the problem was that its not the normal access token, here the access token will be a combination of your app id and app secret separated by "|" symbol. also you need to send the data as an array and not as a json object.

So here is what the working code looks like.

function makePost(personid){
  var address = "https://graph.facebook.com/" + personid + "/notifications";
  var tempdata = {};
  tempdata['access_token'] = appId + "|" + appSecret;
  tempdata['href'] = "";
  tempdata['template'] = "You have earned 5 credits, Click here to redeem";
  jQuery.post(address, tempdata , function(data){
      console.log(data);
  });
}

PS: I am using Jquery Library to make the post request, So dont forget to include jquery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM