Search code examples
javascriptfacebookfacebook-app-requests

Delete Facebook apprequest (using javascript sdk) after a user has been taken to canvas page


I know I need to delete the apprequests after a user has clicked on them, but I can't figure out how to time it right using the Javascript SDK for Facebook.

Right now, I can get the apprequests to delete immediately after they are created, which obviously isn't good because the apprequests vanish before the user has the chance to click on them.

How do I detect that a user has accepted the apprequest, and where do I put my code to respond to it?


Solution

  • https://www.fbrell.com/fb.ui/apprequests

    this code removes all requests your app sent to the current user(put it in your default page)

    this is not best but because i'm not familiar with your coding this is all i did

    this code only works if user installed your app

    <div id="fb-root"></div>
    
    <script>
      window.fbAsyncInit = function() {
        // init the FB JS SDK
        FB.init({
          appId      : 'YOUR_APP_ID',                        // App ID from the app dashboard
          channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel file for x-domain comms
          status     : true,                                 // Check Facebook Login status
          xfbml      : true                                  // Look for social plugins on the page
        });
    
        // Additional initialization code such as adding Event Listeners goes here
      };
    
      // Load the SDK asynchronously
      (function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/en_US/all.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));
    </script>
    <script>
    
          FB.api('/me/apprequests', function(response) {
            var ids = [];
            for (var i=0, l=response.data.length; i<l; i++) {
              FB.api('/' + response.data[i].id, 'DELETE', Log.info.bind('clear requests'));
            }
          });
    
    <script>