1

I am trying to send iOS push notifications from a PHP App Engine backend but I am receiving the following errors. I am not sure if there is a problem with the certificate, the way I'm doing it, or something specific to App Engine. This is my first time sending push notifications to iOS.

Here is my code :

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');

$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
{
  //Handle Error
}

$body['aps'] = array(
  'alert' => $data["message"],
  'sound' => 'default',
);
$body["postID"] = $data["postID"];
$body["groupID"] = $data["groupID"];
$body["type"] = $data["type"];

$payload = json_encode($body);

foreach ($registrationIds as $registrationID)
{
  $deviceToken = $registrationID;
  $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
  $result = fwrite($fp, $msg, strlen($msg));
}

fclose($fp);

Here is the error I get :

PHP Warning:  stream_socket_client(): SSL operation failed with code 1. OpenSSL 
Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake 
failure in fakefile.php

I have no idea what the cause is. Thanks in Advance

UPDATE : That is no longer the error I get. Now I get this :

PHP Warning:  stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error)
TheSabby
  • 267
  • 1
  • 3
  • 12
  • What was the permission of APNS folder and is it a nested permission? – Nimit Parekh May 27 '15 at 11:02
  • I have no control over the folder permissions in app engine but to my knowledge people have successfully implemented push notifications on app engine so I assume the permissions are correct. – TheSabby May 27 '15 at 11:07
  • http://stackoverflow.com/questions/7453015/ios-push-notification-problem-when-using-crontab-scheduler – abh May 27 '15 at 11:37
  • Are you still facing this issue? Were you able to provide your own [local cert](http://php.net/manual/en/context.ssl.php)? Are you aware of the [limitations of sockets on App Engine](https://cloud.google.com/appengine/docs/standard/php/sockets/#limitations_and_restrictions) such as not being able to bind on specific IP addresses or ports? – Nicholas Mar 10 '17 at 00:19

2 Answers2

0

May be your port 2195 and 2196 are closed from server side that are blocking push notifications.

abh
  • 1,189
  • 2
  • 14
  • 30
  • http://stackoverflow.com/questions/7453015/ios-push-notification-problem-when-using-crontab-scheduler – abh May 27 '15 at 11:37
  • check address of ck.pem file... ck.pem file is extracted from apple development or distribution certificate.... these certificates should have permission of push notifications.. check from apple side if this is the problem – abh May 27 '15 at 11:40
  • I checked it. That is not the problem @Babar – TheSabby May 27 '15 at 11:55
0

In stream_context_set_option you did not include the full path to the ck.pem file . After giving the full path there was no error.

Following is code may help to solve your problem.

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '/Users/Development/Dev/ck.pem');

Following code for checking port is enable for Push notification.

<?php

chkServer('gateway.sandbox.push.apple.com', 2195);
//chkServer('gateway.push.apple.com',2195); 

function chkServer($host, $port)
{
    $hostip = @gethostbyname($host);

    if ($hostip == $host) {
        echo "Server is down or does not exist";
    } else {
        if (!$x = @fsockopen($hostip, $port, $errno, $errstr, 5)) {
            echo "Port $port is closed.";
        } else {
            echo "Port $port is open.";
            if ($x) {
                @fclose($x);
            }
        }
    }
}
?>

Happy coding.

Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72