danrodrigues
danrodrigues

Reputation: 181

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?

Upvotes: 0

Views: 370

Answers (2)

montymxb
montymxb

Reputation: 86

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.

Upvotes: 1

Ran Hassid
Ran Hassid

Reputation: 2788

client push is not supported in parse-server (Read here) so in order to send push notifications you must do the following:

  1. Create a cloud code function that will leverage the parse js SDK. there you will need to write some lines of code to send the push to the specific client (Read here

  2. Trigger the cloud code function in your PHP code via parse PHP SDK by sending the relevant parameters to the cloud code function that you trigger

Upvotes: 0

Related Questions