iOS Push Notification PHP error - php

Hello I try to send Push Notifications via PHP over the FTP-Hoster square7.ch, but I always get a few errors. The certificate is on the server.
Can anyone help me?
Here's the PHP code:
<?php
$streamContext= stream_context_create();
stream_context_set_option($streamContext , 'ssl','local_cert' , 'TestPushApp.pem');
//stream_context_set_option($streamContext , 'ssl' , 'passphrase','password');
$socketClient = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$error,$errorString,60,STREAM_CLIENT_CONNECT,$streamContext);
$payload['aps']= array('alert' => 'Erste Push Nachricht ueber PHP','sound' => 'default','badge' => '20');
$payload= json_encode($payload);
echo $payload;
$deviceToken = str_replace(' ','','XXXXXXXXXXXXXXXXXX');
$message= pack('CnH*',0,32,$devicetoken);
$message= $message . pack ('n',strlen($payload));
$message= $messgae . $payload;
fwrite($socketClient,$message);
fclose($socketClient);
?>
Here's the error message:
Warning: stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /users/michaellll/www/push.php on line 5
Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /users/michaellll/www/push.php on line 5
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /users/michaellll/www/push.php on line 5
{"aps":{"alert":"Erste Push Nachricht ueber PHP","sound":"default","badge":"20"}}
Warning: fwrite() expects parameter 1 to be resource, boolean given in /users/michaellll/www/push.php on line 13
Warning: fclose() expects parameter 1 to be resource, boolean given in /users/michaellll/www/push.php on line 14

// Push Notification code for IPHONE in PHP
$deviceToken = $users_rows['gcm_regid'];
$passphrase = 'pass1234';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'DrinksterDevelopment.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 120, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
$body['aps'] = array(
// 'alert' => $_GET["message"].'#'.$_GET["type"].'#'.$_GET["deal_id"],
'alert' => $_GET["message"],
'sound' => 'default'
);
$body['other'] = $_GET["type"].'#'.$_GET["deal_id"];
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result_iphone = fwrite($fp, $msg, strlen($msg));
if (!$result_iphone)
$msg_iphone = 'Message not delivered' . PHP_EOL;
else
$msg_iphone = 'Message successfully delivered' . PHP_EOL;
mail('jackbrown00001#gmail.com', 'IOSPushMsgStatus', $msg_iphone);
fclose($fp);
}

I think its SSL issue, enable SSL then it might work. One more thing is there stream_context_set_option($streamContext , 'ssl','local_cert', 'TestPushApp.pem');
it must be like this
$options = array('ssl' => array('local_cert' => 'include/TestPushApp.pem','passphrase' => '//Provided passpharase to you'));
stream_context_set_option($streamContext , $options);
and if you are having production certificate i. e pem file then its good to use ssl://gateway.push.apple.com:2195
Here file writes function will contain two params only fwrite($fp, $msg); not the length of that packaged message.

Related

iOS push using Zend Framework not working

I have implemented Push notification feature in my iOS Application. Push notification message will be pushed from the backend side.
I am using Zend framework on backend side.
Now If I do online tests with push notification it works. So its sure that .pem file created correctly & on iOS side it received push messages correctly.
But while sending push message from backend from zend framework its not working. Its showing the error message like this :
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 /home/virtual/site71/fst/var/www/html/application/configs/functions.php on line 316
Warning: stream_socket_client(): Failed to enable crypto in /home/virtual/site71/fst/var/www/html/application/configs/functions.php on line 316
Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/virtual/site71/fst/var/www/html/application/configs/functions.php on line 316
Here is implementation :
function sendIOS($deviceToken){
$passphrase = '12345678';
// Put your alert message here:
$message = 'New stock has been added by Dividend Stock!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert_new.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' => array(
'body' => $message,
'action-loc-key' => 'dinero App',
),
'badge' => 1
);
// 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;
fclose($fp);
}
Any guidance or hint will be highly appreciated.

Push Notifications in IOS - Handshake Failure

Whenever I am trying to give Push notifications it throws me multiple error,
I have researched very well yet no solution.
1.stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure
-
2.Severity: Warning
Message: stream_socket_client(): Failed to enable crypto
-
3.Severity: Warning
Message: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error)
-
4.Failed to connect: 0
Below is my code:
// Sends Push notification for iOS users
public function iOS($data, $devicetoken)
{
$deviceToken = $devicetoken;
$passphrase= "";
$ctx = stream_context_create();
// ck.pem is your certificate file
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// 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);
// Create the payload body
$body['aps'] = array(
'alert' => array(
'title' => $data['mtitle'],
'body' => $data['mdesc'],
),
'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));
// Close the connection to the server
fclose($fp);
if (!$result)
return 'Message not delivered' . PHP_EOL;
else
return 'Message successfully delivered' . PHP_EOL;
}
Can anyone help me to sort it out?
add following line
stream_context_set_option( $ctx , 'ssl', 'verify_peer', false);
if you have set password while generating pem file then you have to mention it in following line
$passphrase= "";
if you have not set then this error due to pem file.Its not generated properly.

Error when try to excute Apple Push Notification through php file

I'm working on push notification in IOS with php server and I produced the certificate and keys of the app also I'm ensure from unblocking port for ssl://gateway.sandbox.push.apple.com:2196 and 2195 but at all time I get this error during try to connect on ssl also I'm sure from the permission of all key files
Warning: stream_socket_client(): SSL: crypto enabling timeout in /Users/samahahmaed/Desktop/CER/newspush.php on line 24
Warning: stream_socket_client(): Failed to enable crypto in /Users/samahahmaed/Desktop/CER/newspush.php on line 24
Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/samahahmaed/Desktop/CER/newspush.php on line 24
Failed to connect: 0
Edited:
When I'm trying this command
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushCertificate.pem -key PushKey.pem -CApath /etc/ssl/certs/Entrust_Root_Certification_Authority.pem
I get this error
CONNECTED(00000003)
write:errno=54
php file:
<?php
// Put your device token here (without spaces):
$deviceToken = 'mydevicetokenhere';
// Put your private key's passphrase here:
$passphrase = '1234';
$message = $argv[1];
$url = $argv[2];
if (!$message || !$url)
exit('Example Usage: $php newspush.php \'Breaking News!\' \'https://raywenderlich.com\'' . "\n");
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apple_push_notification_production.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// 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',
'link_url' => $url,
);
// 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);
I searched a lot about this issue and I tried all possible solutions but without any result what I can do now ?
Edited:
After add -debug to openssl command the most strange thing these lines:
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apple_push_notification_production.pem');
This line looks like you are using production certificate to connect to sandbox APNS. Try using development certificate. You can get the same from apple developer dashboard.

APNS integrate in php to send n-number of devices

I have tried with below code to send ios push notification for multiple devices,(It's working for single device if i use without loop).
$devices = array();// Minimum 2k device ids
$ctx = stream_context_create();
// ck.pem is your certificate file
stream_context_set_option($ctx, 'ssl', 'local_cert', $_SERVER['DOCUMENT_ROOT']."/assets/ck.pem");
//stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'key');
// 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);
/* $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);
// Create the payload body
$body['aps'] = array(
'alert' => array(
'title' => $msg1['title'],
'body' => $msg1['message']
),
'vibrate' => 0,
'sound' => 'sound'
);
$results = array();
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
foreach($devices as $device){
if(strlen($device) == 64) {
$msg = chr(0) . pack('n', 32) . pack('H*', $device) . pack('n', strlen($payload)) . $payload;
try {
$results[] = fwrite($fp, $msg, strlen($msg));
}catch (Exception $ex) {
// try once again for socket busy error (fwrite(): SSL operation failed with code 1.
// OpenSSL Error messages:\nerror:1409F07F:SSL routines:SSL3_WRITE_PENDING)
sleep(5); //sleep for 5 seconds
$results[] = fwrite($fp, $msg, strlen($msg));
}
}
}
// Close the connection to the server
fclose($fp);
if (empty($results))
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
But this returns below error, Please help any one to get success from this
A PHP Error was encountered
Severity: Warning
Message: fwrite(): SSL: An existing connection was forcibly closed by the remote host.
Filename: controllers/C_booking.php
Line Number: 265
A PHP Error was encountered
Severity: Warning
Message: fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry
Filename: controllers/C_booking.php
Line Number: 265
A PHP Error was encountered
Severity: Warning
Message: fwrite(): SSL: An established connection was aborted by the software in your host machine.
Filename: controllers/C_booking.php
Line Number: 265

PHP fails socket connection with Apple notification gateway

Disclaimer: on SO I found many similar questions and some answers but none solved my issue.
I have this easy PHP code:
<?php
$deviceToken = "myDeviceToken";
$message = "Hello from server";
$badge = 1;
$sound = "default";
// Construct the notification payload
$body['aps'] = array(
'alert' => $message
);
if ($badge) {
$body['aps']['badge'] = $badge;
}
if ($sound) {
$body['aps']['sound'] = $sound;
}
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'my.pem');
$fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstrn";
return;
} else {
print "Connection OK\n";
}
$payload = json_encode($body);
$msg = chr(0) . pack("n", 32) . pack("H*", str_replace(" ", "", $deviceToken)) . pack("n", strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
?>
When called from browser I receive:
[13-Dec-2011 18:52:52] PHP Warning: stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /home/srv/public_html/myTest.php on line 28
[13-Dec-2011 18:52:52] PHP Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /home/srv/public_html/myTest.php on line 28
[13-Dec-2011 18:52:52] PHP Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/srv/public_html/myTest.php on line 28
Any idea of what's going on?
Some random finding from the internet which could help:
It may be a certificate problem. Try the stream options allow_self_signed and verify_peer to check that.
Try to use explicitely sslv2:// or sslv3:// ?
Permission problem on "/dev/urandom"

Categories