5
votes

I’m trying to do a notifications system for apple devices, but I’m getting the following errors when I try to run it on the server:

Warning: stream_socket_client(): SSL: Connection reset by peer in /home/empresa/public_html/simplepush/push.php on line 30

Warning: stream_socket_client(): Failed to enable crypto in /home/empresa /public_html/push/push.php on line 30

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/empresa /public_html/push/push.php on line 30 Failed to connect: 0

My code is this:

  <?php
ini_set('display_errors','On'); 
error_reporting(E_ALL);
$deviceToken= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';      
$passphrase = ' ';
$message = 'my first notification';
////////////////////////////////////////////////////////////////////////////////
$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);

What could be happening? Thanks.

1
Im using the same routine that you've posted as your source code... almost identical to your usage... It worked for months and quickly sent out notifications to all our iOS users, but i noticed a couple of weeks ago it stopped... It DOES send fine if I do a test to one or two devices though... None of the device IDs are getting reported back to me as invalid (I have a process that automatically removes any device ids that are invalid from our mysql databse). Did you ever resolve the issue on your end?tamak
In doing some additional testing, all the device IDs are valid... it fails at a random ID so its looking like the connection / stream gets dropped ... (which explains why it works fine when theres just a few Device IDs ... the problem occurs when theres over 100 or so devices being sent a notification msg... So I guess I need to write a function that does the connection to the APNS server and then if (in the device ID loop) I see there is no connection, re-open that connection and resume sending? I dont know... still scrambling to get this fixed!tamak
Got same errors. Any suggestion how you guys dealt with this problem?FaNtAMode

1 Answers

1
votes

I can't be sure of a particular reason

But please make sure, you are not doing any of the below things wrong:

  • Don't make many connections in parallel. Either reuse the same connection or close the connection after delivering Push Notifications. Actually, servers have a limit for maximum number of parallel connections, which might leave you in trouble, once you reach threshold. Also Apple suggests leave a connection open unless you know it will be idle.

    Keep your connections with APNs open across multiple notifications; don’t repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack. You should leave a connection open unless you know it will be idle for an extended period of time—for example, if you only send notifications to your users once a day it is ok to use a new connection each day.

  • Don't send out developer profile tokens to LIVE APNS. Keep distribution and development app tokens separate. It could result in error, if you try to send sandbox tokens to LIVE APNS or vice versa.

SOURCE - APNS - notifications push ios: Connection reset by peer PHP