如何使用 Parse Server 和 PHP 发送推送通知

How to send a push notification using Parse Server and PHP

我一直在努力让它工作一整天,但没有成功。这就是我的进展...

 <?php  
 $url = 'http://parse.verbomedia.com:1337/parse/push';  
 $appId = '******';  
 $masterKey = '******';  
 $headers = array(  
   "Content-Type: application/json",  
   "X-Parse-Application-Id: " . $appId,  
   "X-Parse-Master-Key: " . $masterKey
 );  
 $objectData = '{"where":{"deviceType":"ios"},"data":{"alert":"Hello, Parse!"}}';  
 $rest = curl_init();  
 curl_setopt($rest,CURLOPT_URL,$url);  
 curl_setopt($rest,CURLOPT_POST,1);  
 curl_setopt($rest,CURLOPT_POSTFIELDS,$objectData);  
 curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);  
 curl_setopt($rest,CURLOPT_SSL_VERIFYPEER, false);  
 curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);  
 $response = curl_exec($rest);  
 echo $response;  
 print_r($response);  
 curl_close($rest);  
 ?>  

通过 Parse Dashboard 运行良好。

关于我可能做错了什么有什么想法吗?

解析服务器 (Read here) 不支持客户端推送,因此要发送推送通知,您必须执行以下操作:

  1. 创建一个将利用 parse js SDK 的云代码函数。在那里你需要编写一些代码行来将推送发送到特定的客户端(Read here

  2. 通过解析PHP SDK,将相关参数发送到您触发的云代码函数,在您的PHP代码中触发云代码函数

php sdk 当前支持通过 ParsePush class 进行客户端推送。以下内容摘自 php sdk 的 github 页面上的 README.md 中的示例。

// Push to Channels
ParsePush::send(array(
    "channels" => ["PHPFans"],
    "data" => $data
), true);

// note the master key is required via 'true'

您也可以在 docs 中找到此 SDK 的其他示例和说明。

请注意,尽管 php sdk 支持此功能,但您必须 首先 在您的解析服务器实例中设置一个 working push configuration。尽管 php sdk 开箱即用,但如果没有提前正确设置,服务器将无法处理通过 GCM 或 APNS 推送通知。