Sending multiple iPhone push notifications + APNS + PHP
You should better use APNS library for PHP. You can find it here. Look through samples that developers provide.
I also had problems with certificates. My actions were:
- locate file
ApnsPHP/Abstract.php
make some changes to
_connect()
method, paste this lines$streamContext = stream_context_create( array( 'ssl' => array( 'local_cert' => $this->_sProviderCertificateFile, 'passphrase' => '' ) ) ); $this->_hSocket = @stream_socket_client( $sURL, $nError, $sError, $this->_nConnectTimeout, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $streamContext);
instead of original listed there
now you can use *.pem certificates without need of entrust_root_certification_authority.
This worked fine for me.
Simple way to do it without use any file. You can call it multiple times with different tokeid.
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ckipad.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$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 amarnew: $err $errstr" . PHP_EOL);
//echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'badge' => +1,
'alert' => $message,
'sound' => 'default'
);
$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 amar'.$message. PHP_EOL;
// Close the connection to the server
fclose($fp);