I would like to send push messages from my website to iOs/android users everytime I publish a new post. Which would be the same as execute php code I think. I have tried to paste a php script which send a push message to my iOs device everytime I launch it, but without success. I've pasted the php code right after this code
<div id="content"> <?php if( have_posts() ): get_template_part( 'templates/content/content', str_replace('bw_pt_', '', get_post_type() ) );
In the single.php file in word press->Appearance->editor. As well as I have uploaded the ck.pem file in the same folder. This is my php code and when I launch it from the terminal, it works well. What I need is to add some custom code, which would be a retrieve database to see all device and to send a push notification for each device. I'll do the code later, for the moment I need to know were I have to put this code, in order to be executed everytime the admin press the publish new entry button.
This is my php code:
<?php // Put your device token here (without spaces): $deviceToken = '993c92d21b053c5115d1xxXXXXxxXXXXXxxxXXXXXxxebe9c21f0f59a0723de8bd38'; // Put your private key's passphrase here: $passphrase = '123456'; // Put your alert message here: $message = 'Hello world!'; //////////////////////////////////////////////////////////////////////////////// $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); // Open a connection to the APNS server $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL); echo 'Connected to APNS' . PHP_EOL; // Create the payload body $body['aps'] = array( 'alert' => $message, 'sound' => 'default' ); // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; // Close the connection to the server fclose($fp); ?>