iOS push using Zend Framework not working - php

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.

Related

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.

Ios push-notification PHP script not working for Ubuntu 14.04

I'm working on an iOS app that use iOS Push Notifications. I want to send the notification from a php script on my windows PC. I use this php script to send notification and it works well too:
// Put your device token here (without spaces):
$deviceToken = 'sdsdsdsdsczc2';
$sound = '';
// Put your private key's passphrase here:
$passphrase = 'awertf';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
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.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(
'badge' => +1,
'alert' => $message,
'sound' => $sound
);
// 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);
This php script works well on windows but now i'm working on Ubuntu 14.04 operating system and same php script is giving me error.
Message: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
I guess it's something related with port 2195. But how to solve this!
This was an enormously helpful link to find:
http://php.net/manual/en/migration56.openssl.php
http://php.net/manual/en/context.php
An official document describing the changes made to openssl in PHP 5.6
It's usefull to learn one more parameter that Tou should have set to false/true: for example "verify_peer_name"=>false
So some code looks like this:
<?php
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$response = file_get_contents('ssl://'.$host.':'.$port, false, stream_context_create($arrContextOptions));
echo $response; ?>

why does Apple push server think I am using SSLv3 when I specify TLS? (apple push switch to tls)

I apologize in advance, but I am asking the same question as here: How to send iOS Push Notifications using TLS and PHP? because the problem remains unresolved. Here is the Ray Wenderlich tutorial php code I am using to try to send a push notification, as modified per SO post's recommendations:
<?php
// Put your device token here (without spaces):
$deviceToken = '325bcd9d15ab8b5f8ff6f4f3d5ab3a5254f1ce775d48828476f4aaff4c5c3d3f';
// Put your private key's passphrase here:
$passphrase = 'pushchat';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');
// Open a connection to the APNS server
$fp = stream_socket_client(
'tls://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);
I also downloaded the entrust_2048_ca.cer file and ensured that it shows up on the System tab of my keychain access. When I run this php file, I still get the following errors, which is the same set of errors I got before making changes to reflect use of tls:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /Users/aileenann/Desktop/SimplePush/simplepush.php on line 22
Warning: stream_socket_client(): Failed to enable crypto in /Users/aileenann/Desktop/SimplePush/simplepush.php on line 22
Warning: stream_socket_client(): unable to connect to tls://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/aileenann/Desktop/SimplePush/simplepush.php on line 22
Failed to connect: 0
What else needs to be changed? Why does the tls apple server still think I am trying to use SSL3 when I have specified tls in the URL?
Thank you for any suggestions.

iOS Push Notification PHP error

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.

Categories