Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notifications push ios: Connection reset by peer

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.

like image 845
Luis Bolívar Avatar asked Nov 10 '22 08:11

Luis Bolívar


1 Answers

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

like image 183
Rythm Avatar answered Dec 27 '22 21:12

Rythm