0

I know there are many questions for this but I have tried doing all the suggestions with no luck, I have the below error when running my push.php in shell command., I am following this tutorial and everything worked untill the php. i recreated the certificates and keys many many times and since it's letting me connect using openssh then i guess the problem is not with it. today i have again recreated all the certificates and did a new project now I don't even receive the first prompt of do you accept to allow push notification in this app. I am stuck I am on a deadline and was facing this problem for week, please help me, my php is exact same file as the tutorial with modified token and passcode I have added the path to ck.pem as was suggested by some of the S.O.answers but not working yet.also I have added CA file entrust_2048_ca.cer , it didn't help resolving the issue. and all this is using development certificate and not yet on a server, what might be the issues your ideas? is using external library will be better ? Thank you for your help in advance.

Warning: stream_socket_client(): Unable to set private key file `/Users/.../..../.../.../ck.pem' in /Users/dalyaseen/Documents/test3d/test3d/push.php on line 19

Warning: stream_socket_client(): failed to create an SSL handle in /.../..../.../.../push.php on line 19

Warning: stream_socket_client(): Failed to enable crypto in /Users/.../..../.../.../push.php on line 19

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/.../..../.../.../push.php on line 19 Failed to connect: 0

here is the php script

<?php
#$deviceToken='ac49c9c8 a7d7f340 1c5c9af8 b8afeada ee7cdb77 051f12e8 90c314e1 129af45e';
$deviceToken='ac49c9c8a7d7f3401c5c9af8b8afeadaee7cdb77051f12e890c314e11y8u89i9o0lp';
# Put your private key's passphrase here:
$passphrase = '1234';

# Put your alert message here:
$message = 'push msg!';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '/Users/d/Documents/test/test/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'cafile', '/Users/d/Documents/test/test/entrust_2048_ca.cer');
#stream_context_set_option($ctx, 'ssl', 'allow_self_signed', 1);
#stream_context_set_option($ctx, 'ssl', 'verify_peer', 1);

 //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' => $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);

?>
Dina
  • 467
  • 4
  • 18
  • Some google search gives me this http://stackoverflow.com/questions/21013848/sending-ios-notification-through-a-php-script-unable-to-set-private-key-file, http://stackoverflow.com/questions/1762803/apn-error-in-server-script – Yogesh Suthar Jul 22 '14 at 03:54
  • can u put it some code from you php script, and also in the mentioned tutorial you will get a simplepush.php file, try replacing your device token in it and run the script, if you receive a nori. then all your certificate are proper and its only a server issue, if not, then u probably messed up the certificates and keys.. – Geet Jul 22 '14 at 07:39
  • Also see [“verify error:num=20” when connecting to gateway.sandbox.push.apple.com](http://stackoverflow.com/a/23351633/608639). You should ensure three things: (1) TLS 1.0 or above; (2) Server Nam Indication; (3) *Entrust.net Certification Authority (2048)* root. – jww Nov 14 '16 at 06:43

1 Answers1

5

Try This Code :

<?php

$deviceToken='ac49c9c8a7d7f3401c5c9af8b8afeadaee7cdb77051f12e890c314e11y8u89i9o0lp';
$passphrase = '1234';
$message = 'push msg!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '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: $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)
    return 'Message not delivered' . PHP_EOL;

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

IMP: Put your ck.pem file parallel to this code. Once your code works you can change the position of your .pem file.

Make Sure the certificate you created is in "Developer" Mode Also, the device is added to the provisioning profile.

siddarth
  • 51
  • 1
  • 4