I've been searching for weeks about my problem, but I can't find a solution. I've tried everything related to iOS push notifications service, but nothing works. Here's my problem:
When uploading my php
file and .pem
file to my server, I get a timed out error
when sending the notification.
My production certificate is 100% working, because I test the script on my local machine and it sends the notification.
I'm using the right URL:
gateway.push.apple.com
and the right port2195
I know the device token changes when I'm on production.
My server doesn't have Firewall and it has
17.xxxx
ports opened.I tested notifications in development and it worked on the server.
I'm using an absolute path to my certificate
/home/project/public_html/ck.pem
So I just want to know if there's any new configuration that I have to do on my server when changing from development to production, because it worked some time ago with dev certs.
Note that notifications work on my local machine with MAMP
and with production cert.
EDIT
This is the code I use to send a simple notification:
<?php
$deviceToken = 'DISTRIBUTION TOKEN';
$passphrase = '*******';
$message = 'Hey! It's working';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '/home/project/public_html/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.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);
?>