简体   繁体   中英

Using PHp SDK4 with Javascript SDK to send notifications

I am building a Facebook canvas application. I did most of the parts using the javascript SDK, and now I am stuck at where I would probably need to use PHP SDK 4 for. I am trying to send notifications to my users. For this purpose, I did some research and could find only limited documentation available. I will summarize through points.

  1. I performed all the actions like logging in, retrieving user profile etc etc in JavaScript SDK.

  2. Need to use PHP SDK4 for sending out notifications. The following is what I have done so far.

Imported all the required files like require_once( 'Facebook/HttpClients/FacebookHttpable.php' ); and used them like use Facebook\\HttpClients\\FacebookHttpable; [ I triple cross checked the paths. They are all correct].

Set a Facebook session like follows.

FacebookSession::setDefaultApplication('xxxxxxx','xxxxxxxx');

Obtained a new app access token like follows.

$appSession = FacebookSession::newAppSession();

Tried to use Graph API to do notification

$notification = new FacebookRequest(
$appSession,
'POST',
'/{777911845584420}/notifications',
array (
    'href' => "apps.wehubs.com/ashifshereef/amritham2/againsample/again2/",
    'template' => 'You have been tagged, please set privacy preference',


)
);
$response = $notification->execute();
$graphObject = $response->getGraphObject();

I am getting nothing. No Notifications, no anything. I know there is something strange with this code. Something to add, or something not done the right way. I am pretty well versed in javacsript, but not PHP. My Question is, clear. How to make this thing work? Sorry for the junks, Its all I could come up with, with the pretty shitty Documentation available on PHP SDK 4.

I would just use CURL for App Notifications instead:

$accessToken = $appId . '|' . $appSecret;
$notificationText = 'The cake is a lie.';

//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook');

//send notification
$url = 'https://graph.facebook.com/' . $userId . '/notifications' .
    '?access_token=' . $accesstoken .
    '&template=' . $notificationText .
    '&href=';
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);

Source: http://blog.limesoda.com/2014/08/app-notifications-facebook-apps/

As long as the user authorized your App, this should be fine. And keep in mind that you will not see the App Notifications on mobile devices.

One more thing: "apps.wehubs.com/..." is incorrect, it would need to be the absolute URL - or a relative URL for the Canvas (apps.facebook.com/yournamespace/relativeurl). Not sure though if it even works with an external URL, because App Notifications are for Apps on facebook.com only ( https://developers.facebook.com/docs/games/notifications ).

This works for me:

<?php

require ('facebook-sdk/autoload.php');
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
use Facebook\Entities\AccessToken;
use Facebook\GraphUser;

session_start(); 
?>

<html>
<head>
</head>
<body>
<div>Testing Notifications</div>
<?php 
try {

    FacebookSession::setDefaultApplication(
        "{your_app_id}",
        "{your_app_secret}"
    );

    $appSession = FacebookSession::newAppSession();


        $request = new FacebookRequest(
            $appSession,
            'POST',
            '/'. '{user_id}' .'/notifications',
            array (
                'href' => "index.php",
                'template' => '{your_notification_text}',
            )
        );

        $response = $request->execute();
        $graphObject = $response->getGraphObject(); 

        echo 'Success?: ' . $graphObject->getProperty('success');


}catch(FacebookRequestException $ex) {
    echo 'FacebookRequestException: ' . $ex->getMessage();

}catch(\Exception $ex) {
    echo 'Exception' . $ex->getMessage();
} 
?>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM