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?