Please wait for 15 seconds...

Friday, 21 December 2012



In this tutorial I will show you how to send private message to facebook user using Javascript Facebook API. Although it looks very complicated but in real it is very simple, just follow the tutorial. So let's get start it.

First insert facebook javascript library in your HTML page. For this, copy below code and do not forget to replace APP_ID with your Facebook App ID and www.mysite.com with your domain name. Although you can insert below codes anywhere in HTML page but I would recommend you to insert them just before </head>



Load Facebook JS API Asynchronously


<script type="text/javascript">
// Load the SDK Asynchronously
(function(d){
       var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
       if (d.getElementById(id)) {return;}
       js = d.createElement('script'); js.id = id; js.async = true;
       js.src = "//connect.facebook.net/en_US/all.js";
       ref.parentNode.insertBefore(js, ref);
}(document));
</script>

Initializing Basic Settings for this APP


<script type="text/javascript">
window.fbAsyncInit = function() {
      FB.init({
        appId      : 'APP_ID',
        channelUrl : '//www.mysite.com/channel.html', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
      });
};
</script>

Now create send private message function

<script type="text/javascript">
   function send_private_msg_to_fb_user(){
FB.login(function(response){
if (response.authResponse){
FB.ui({
method: 'send',
  name: 'Send Private Message to Facebook User using Javascript Facebook API',  
link: 'http://www.techsirius.com/2012/12/send-private-message-to-facebook-user.html',  
description: 'In this tutorial I will show you how to send private message to facebook user using Javascript Facebook API. Although it looks very complicated but in real it is very simple, just follow the tutorial.'
});
}
});
}
</script>

Set Meta for Facebook

These two meta fields are required for Facebook 

<meta property="og:url" content="CURRENT_PAGE_URL"/>
<meta property="og:title" content="TITLE_OF_THE_PAGE"/>

Replace CURRENT_PAGE_URL with your page URL and TITLE_OF_THE_PAGE with page title.

Note:
1) Test this on TLD like www.techsirius.com/test.html or demo.techsirius.com/test.html. It will throw error on local server.
2)  Make sure your Facebook app is open for public, it should not be in development mode, in Facebook app setting.

Posted by Atul Prasad Gupta

0 comments:

Post a Comment

Subscribe Techsirius

Techsirius on Facebook