phpfacebookfacebook-graph-apicurlfacebook-app-requests

Facebook app notification template with user id ( @[user_id] ) not working


I can't get the @[user_id] template to work using:

$userid = <USER_ID>;
$friendId = <FRIEND_ID>; // This is a user who has accepted the app
$url = "https://graph.facebook.com/$userid/notifications";
$attachment =  array(   
                 'access_token'  => "$app_token",
                 'href' =>  "",
                 'template' => "@[" . $friendId . "] accepted challenge!"
               );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
//curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close ($ch);

$result returns 'false'

$friendId is a valid app user and $app_token is "appid|appsecret"

If I replace 'template' with a simple message such as "test" it all works fine

Any ideas?


Solution

  • You're probably using a wrong user ID. In order to make sure you're using the appropriate ID for your friend, use me/friends with a valid user access token for your App and check if the ID is in the list and if it's a normal user id or an app scoped ID.

    Please also make sure your App is not in "Developer Mode", if so, you will need to add your user as tester or developer so you can test your App.

    I've tested this a couple of minutes ago and it works well.

    As a unrelated question, wouldn't be easier use Facebook's PHP SDK?

    I hope it helps.


    Edit: App Scoped User ID

    Facebook introduced versioning in April 2014. In Facebook API V2, we introduced the App Scoped User ID, you can read more about it here: https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids

    Basically, if your FB App was created after April 2014, you'll receive a unique ID for each user that can be used only by your App. You must to use this new App Scoped User ID instead of the normal Facebook User ID.