1

I had succeeded earlier on sending a push notification to my iPhone using raywenderlich's excellent tutorial here http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 and running the php script using the mac terminal, however, now I am trying to achieve the same thing only now I have the push.php script uploaded to my server and run the script every 5 min via Cron Job but i get this error now:

stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out)

Like I said before it was working fine earlier when I was running the script through the terminal. I think the problem is that the php script from the server cannot access my push certificates. How do I give the script access to them? Please help!

Heres the php script if it helps:

<?php


// Assign data into variables
$deviceToken = "e39ffc6b98f649f127d07d2d881bc9faa621a3c5d59f9647e64f6452fc37af6c";
$passphrase = "9q3n6k80";
$sound = '';
$blank = "";

$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' => "mic check",
    '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 succesfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp); 

Thank You!

EDIT: Im using the File Manager in Bluehost to upload files

Garret Kaye
  • 2,412
  • 4
  • 21
  • 45
  • "run the script every 5 min via Cron Job"? Remember that Apple says "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.". Every 5 minutes would probably not be enough to trigger their DoS filters, but you should avoid the pattern nonetheless. – jcaron Feb 08 '16 at 15:28
  • Right, I actually am not going to run it every 5 min, I run it every hour, but for debugging purposes I made it 5 min so I wouldn't have to wait an hour to see if it works haha and I just simplified the script so it would be easier to find the problem – Garret Kaye Feb 08 '16 at 15:33
  • You may also want to check if your host and/or server do not block ports for some reason. – jcaron Feb 08 '16 at 16:02
  • I'm facing exactly the same issue right now. I'm running the script every 8 minutes. – Heitor May 27 '16 at 03:48
  • 1
    Possible duplicate of [stream\_socket\_client unable to connect (connection timed out)](http://stackoverflow.com/questions/1769189/stream-socket-client-unable-to-connect-connection-timed-out) – Heitor May 28 '16 at 07:01

0 Answers0