Giulio
Giulio

Reputation: 221

PHP IOS push notifications not working for Development

I'm tying to send a push notification in development mode. I've followed this guide http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

and my code is:

<?php

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck_dev.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'xxxxx');

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

if($fp === FALSE){
    exit('error message');
}

// Create the payload body
$body['aps'] = array(
    'badge' => +1,
    'alert' => 'test',
    'sound' => 'default',
    'content-available' => +1
);

$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', 'xxxxxxxxxxxxxxxxxxxxxx') . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
 fflush($fp);
if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered amar'.$msg. PHP_EOL;

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

The problem is that I receive always "Message successfully delivered" also when the password o pem file are wrong or not exists. How to resolve the problem? Where I'm wrong?

Thanks a lot

Upvotes: 0

Views: 671

Answers (1)

greg_diesel
greg_diesel

Reputation: 3005

Do you have error reporting on? I just tried to open your $fp connection and got plenty of errors.

Add

ini_set('display_errors', 1);

to the top of your PHP script.

See this code viper ( http://codepad.viper-7.com/PlsftP )

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /code/nyl94y on line 11

Warning: stream_socket_client(): Failed to enable crypto in /code/nyl94y on line 11

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /code/nyl94y on line 11 PHP Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /code/nyl94y on line 11 PHP Warning: stream_socket_client(): Failed to enable crypto in /code/nyl94y on line 11 PHP Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /code/nyl94y on line 11

Upvotes: 2

Related Questions