0

My setup is a canvas/web page app on facebook that uses the JavaScript SDK to do login with facebook, and then uses the PHP SDK to do some server side stuff. One of the server side functions to to send out a notification when a post gets a new comment. Note, these are posts in the app, not a Facebook feed / comment. I was going to try and use the Facebook Notification Edge but it's currently in beta and only open to canvas apps and only accessed from Facebook.com web page. So instead I thought I would do what games do by sending a message from the comment poster to the post creator using the apprequest edge.

Here is the code that I use to login. - This works great.

FB.init({
    appId      : 'xxxxxx',
    xfbml      : true,
    cookie     : true,
    version    : 'v2.2'
});

function onLogin(response) {
    if (response.status == 'connected') {
        FB.api('/me?fields=id,first_name,last_name,email', function(data) {
            // I do some stuff here that has nothing to do with problem
        });
    }
}

FB.getLoginStatus(function(response) {
    if (response.status == 'connected') {
        onLogin(response);
    } else {
        FB.login(function(response) {
            onLogin(response);
        }, {scope: 'user_friends, email'});
    }
});

I process this on each page refresh to keep the session cookie active for the PHP calls. Everything is good up to this point, I get the GraphAPI data for the user the login prompt comes up as needed and requests the correct authorization.

Now in the PHP I'm sending a test message from a test user to myself. At this point I am logged in as the test user and I hard code my Facebook ID in the 'To' property.

// FB Requires
// Loaded from a seperate script to include all SDK files

// FB NameSpace
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookJavaScriptLoginHelper;

FacebookSession::setDefaultApplication($GLOBALS['FB_AppID'], $GLOBALS['FB_AppSecret']);

// Simple Class to send a notification to a user about a status update
class FBRequest {

    private $session;

    function __construct()
    {
        $helper = new FacebookJavaScriptLoginHelper();
        try {
            $this->session = $helper->getSession();
        } catch(FacebookRequestException $ex) {
            // When Facebook returns an error
            echo "FBExHelper :: " . $ex->getMessage();
        } catch(\Exception $ex) {
            // When validation fails or other local issues
            echo "HelperEx :: " . $ex->getMessage();
        }

        return;

        // If you're making app-level requests:
        $this->session = FacebookSession::newAppSession();

        // To validate the session:
        try {
            $this->session->validate();
            $_SESSION['access_token'] = $this->session->getToken();
        } catch (FacebookRequestException $ex) {
            // Session not valid, Graph API returned an exception with the reason.
            echo $ex->getMessage();
        } catch (\Exception $ex) {
            // Graph API returned info, but it may mismatch the current app or have expired.
            echo $ex->getMessage();
        }
    }

    public function SendRequest()
    {
        if (!$this->session) return;
        /* PHP SDK v4.0.0 */
        /* make the API call */
        $request = new FacebookRequest(
            $this->session,
            'POST',
            "/me/apprequests",
            array (
                'message' => 'This is a test message',
                'to' => '0123456789101112' // My Facebook ID here
            )
        );

        $response = $request->execute();
        $graphObject = $response->getGraphObject();
        /* handle the result */

        var_dump($response);
        echo "<br><br>";
        var_dump($graphObject);
    }
}

When I run this script everything looks good. The var_dump of the $response just shows that the session is set and has a lot of junk that doesn't matter at this point. The var_dump of the $graphObject looks like the documentation shows that it should look, Facebook API /user/apprequest

object(Facebook\GraphObject)#11 (1) {
    ["backingData":protected] => array(2) {
        ["request"] => string(15) "795036370586912"
        ["to"] => array(1) {
            [0] => string(15) "0123456789101112"
        }
    }
}

Now at this point I should be seeing a message / notification somewhere. My phone, the facebook page, the globe icon, my feed, my home, messenger, lights in the sky, flickering moon, carrier pigeon, you know something, but I can't see that an apprequest is ever send anywhere, except that I have a request ID.

Any suggestions what I'm doing wrong, or a better way to send my users notifications?

Dustin Ruckman
  • 103
  • 2
  • 14
  • I found this http://stackoverflow.com/questions/16792782/app-requests-successful-but-no-notification-shown While it references the Javascript API the fix might be related to my issue. My app is still in sandbox, but it is canvas and I am using it in canvas for my testing. I have not turned off sandbox yet to test, but can someone confirm that apprequests do not process if in sandbox mode? – Dustin Ruckman Feb 03 '15 at 19:15
  • apprequests are for games on canvas ("Requests give players a mechanism for inviting their friends to play a game"). you should not abuse facebook functionality for something else. external apps are not meant to use the facebook notification system anyway. – andyrandy Feb 03 '15 at 19:24
  • While my app is not a game it is a Facebook Canvas App that is designed to function inside the Facebook page. Later I will port out to iOS and Android Facebook but for now it's completely contained in the Canvas, but for testing I do have it setup with a WebSite in Facebook also. I know that the apprequest is listed as a game service, but if I use it in the same manner to engage users would that be considered abuse? – Dustin Ruckman Feb 03 '15 at 19:40
  • it´s definitely used in a wrong way. if you got a canvas app, why not use app notifications? it´s beta, but it works great and after all you want to send notifications, right? – andyrandy Feb 03 '15 at 20:18
  • You are right, I'll switch over to Notifications. – Dustin Ruckman Feb 03 '15 at 22:34

1 Answers1

1

After the comments from luschn I have descided to switch over to Notifications since the apprequest is really built for use with games.

However, The issue I was having with the apprequest not showing up was due to the app being set to sandbox (app was not Live) Once I turned on live the requests I tested with sent without any problems.

So anyone with this same issue... The above code does seem to work, use it if you need to.

Dustin Ruckman
  • 103
  • 2
  • 14