tags:

views:

311

answers:

2

When I try to send Push Notifications I get this error: "Connection refused", but I don't know why... I've uploaded my apns-dev.pem in the same directory as well in the root-directory but that won't work either.

<?php
$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';
$apnsPass = 'secret';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $apnsPass);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);

if (!$apns) {
    echo "Error: $errorString ($error)";
}

// Do this for each
$deviceToken = '00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000';
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
// End do

socket_close($apns);
fclose($apns);
?>

Does anyone know what I'm doing wrong? When I remove the passphrase and don't send it it doesn't work either...

+1  A: 

You don't want a passphrase unless your .pem file requires one. The connection requires peer verification (option verify_peer) turned on. Also, make sure $apnsCert is the valid path to the certificate, you can use an absolute path as a sanity check.

Lastly, this shouldn't effect your ability to connect, but your device token needs to be valid.

Ben Lachman
Really thanks, I'm going to check if this works.Do you mean the ftp-path (/public-html/directory/file.pem) or (http://www.server.com/directory/file.pem) by absolute path?I get this error before I send any data, so it's not the device-token, I think...Thanks :D!
Tim van Elsloo
It doesn't work :(..., below is my answer to prevent that error.
Tim van Elsloo
A: 

I've know fixed that error by adding this: STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT

Know I'm not getting any errors, but I don't receive any notification. I think the Dev-token is not valid know, so, this is how it look like

numbers numbers numbers numbers numbers numbers numbers numbers.

The spaces are removed in this line: $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;

Edit: I founded the problem: My server is refusing the outgoing port, just sent a mail, hoping they can activate it...

Tim van Elsloo