3

I am trying to send a push notification in iOS application via php. So the iOS developer give me the pem file. I requested the hosting server to open gateway.sandbox.push.apple.com:2195 and they have opened the port. But when I am trying to send push notification, I am getting the following error. My code looks like

$deviceToken='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

$passphrase="";

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '/home/sitename/public_html/push/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

$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 amarnew: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

Getting the following error

Failed to connect amarnew: 0 

So I tried the connection to apple via telnet, that result is

root@uio3-i [~]# telnet gateway.sandbox.push.apple.com 2195
Trying XX.XXX.XXX.XX...
Connected to gateway.sandbox.push.apple.com.
Escape character is '^]'.
^\q
^]
telnet> q
Connection closed.
root@uio3-i [~]#

As I am new to this, I just stucked here. Please any one help me

Thanks in advance

4

1 回答 1

5

Sounds like your certificate/key pair .pem file is corrupt, regenerate it. I only just got push working myself so I know it's a tricky topic.

Rather than simply telnet (which only proves connectivity is OK), instead run this command from terminal to determine if the Cert and Key are valid.

openssl s_client -connect gateway.sandbox.push.apple.com:2195 
-cert cert.pem -key key.pem

You should see a whole bunch of output, which is openssl letting you know what is going on under the hood.

If the connection is successful, you should be able to type a few characters. When you press enter, the server should disconnect. If there was a problem establishing the connection, openssl will give you an error message but you may have to scroll up through the output to find it.

Either way at least you will know if your cert/key pair are good.

In production you have to use a concatenated cert/key pair - If you don't have the original key and cert you will need to get this from the dev to test the above.

于 2013-08-30T09:30:21.340 回答