Search code examples
phpioscurlpush-notificationparse-server

How to send a push notification using Parse Server and PHP


I've been trying to make it work all day and no luck. This is how far I got...

 <?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);  
 ?>  

It works fine through the Parse Dashboard.

Any ideas on what I might be doing wrong?


Solution

  • The php sdk currently supports client push via the ParsePush class. The following is pulled from an example in the README.md on the php sdk's github page.

    // Push to Channels
    ParsePush::send(array(
        "channels" => ["PHPFans"],
        "data" => $data
    ), true);
    
    // note the master key is required via 'true'
    

    You can find additional examples and explanations in the docs for this sdk as well.

    Note that although the php sdk supports this you must first setup a working push configuration in your parse-server instance. Although the php sdk is good to go out of the box, the server won't be able to handle pushing out notifications via GCM or APNS without the proper setup in advance.