Azuz
Azuz

Reputation: 29

Is there a JavaScript API for sending notifications to Facebook users?

I am currently developing a Facebook application on a website that would need to send a notification to the app's users without using a user interface dialog. After reading some blogs I concluded that the option is available in example in PHP API only. I could only find this example:

http://developers.facebook.com/docs/channels/

Is there a JavaScript API to do this?

After some sort of more reading, I found out that FB.api could handle graph object apis and also the rest apis which are to be deprecated, and I got the following working:

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

However, that id number 1175241653 does not work if the logged in user's id is not that id.

Therefore this would required the same functionaliy that Facebook uses to retrieve the ID of whomever signed into the application. Is there any way to do this?

Upvotes: 1

Views: 1263

Answers (1)

Azuz
Azuz

Reputation: 29

Now , I got this working in all means and I'd like to share it with those who may deal with :))

lets say 1st as to do a single app request from ur application to any of facebook registered users in your application would be like this:

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 should be provided as to authenticate the request from the application to the registered user.

If you do not know about access tokens, you can read about it over at the facebook site:

http://developers.facebook.com/docs/authentication/

Also, if you want to send batch requests to a set of users in one request call, there's a support page from the facebook site about that too:

http://developers.facebook.com/docs/reference/api/batch/

and here's a sample of what I mean :

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

Upvotes: 1

Related Questions