0

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.

  1. My production certificate is 100% working, because I test the script on my local machine and it sends the notification.

  2. I'm using the right URL: gateway.push.apple.com and the right port 2195

  3. I know the device token changes when I'm on production.

  4. My server doesn't have Firewall and it has 17.xxxx ports opened.

  5. I tested notifications in development and it worked on the server.

  6. 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);

?>
mursang
  • 501
  • 5
  • 21

2 Answers2

2

It had happened to me also, the reason turned out to be the fact that my server was blocking 2195 port .. try running this command to your server from terminal

telnet gateway.push.apple.com 2195

you should get the following response Trying 17.172.234.2... Connected to gateway.push-apple.com.akadns.net. Escape character is '^]'.

if you get connection timed out then your server is blocking the port 2195

Community
  • 1
  • 1
Geet
  • 2,417
  • 2
  • 18
  • 39
1

Can you try by changing the php code from

 $message = 'Hey! It's working';

to

 $message = "Hey! It's working";

are you sure that 1.you are passing the correct deviceToken ? 2.using the production certificate in the ios app. 3. at the time of creating the ck.pem for production you have used the public key of CSR to export pem ?

Esha
  • 1,297
  • 12
  • 31