8

我想通过通知 API 从应用程序向已验证我的应用程序的用户发送通知。任何人都可以帮我解决这个问题吗?任何帮助将不胜感激。

可以说,我希望用户在我的应用中发生某些事件时收到通知

提前致谢

4

2 回答 2

27

我自己找到了解决方案。开发者需要遵循以下步骤:

  1. 从https://developers.facebook.com/docs/reference/php/下载 facebook sdk for php
  2. 运行以下代码段:

代码:

require_once "facebook-php-sdk/facebook.php";

$facebook = new Facebook();

$app_id = YOUR_APP_ID;

$app_secret = YOUR_APP_SECRET;

$app_access_token = $app_id . '|' . $app_secret;

$response = $facebook->api( '/RECEIVER_USER_ID/notifications', 'POST', array(

                'template' => 'You have received a new message.',

                'href' => 'RELATIVE URL',

                'access_token' => $app_access_token
            ) );    

print_r($response);

如果一切正常,那么您将获得以下输出:

Array

(

    [success] => 1
)
于 2012-10-12T05:20:41.130 回答
0

这是@Aakash 对此问题的回复的更新代码。

require_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk
$fb = new Facebook\Facebook([
  'app_id' => '18341642425638', // sample app id
  'app_secret' => '1a8a4037df4d49e5712310c8ad7a62f', // sample app secret
  'default_graph_version' => 'v2.12',
]);

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->post('/{user_id}/notifications?access_token=1X34XXXXXX25X38|kxXXxxXXXXX3Y&href=&template=@[user_id] Message to display (180 Characters))', 'user_app_token');

} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

var_dump($response);

$response 变量应该返回一个只有一个元素 "success" => 1 的数组。

此处提供了所有必要的变量。

您可以在此处尝试模拟并向自己发送通知。

这里是文档

于 2018-04-19T14:35:03.483 回答