Im developing iPhone application for push notification and Im getting an error when I run the php file on my localhost. I have created the Certificates, .pem file correctly. And then when I test the app on my localhost host, Warnings are displayed.
Warning: stream_socket_client() [function.stream-socket-client]: Unable to set private key file `/Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/ck.pem' in /Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush.php on line 21
Warning: stream_socket_client() [function.stream-socket-client]: failed to create an SSL handle in /Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush.php on line 21
Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush.php on line 21
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush.php on line 21 Failed to connect: 0
Im getting this errors. What might be the cause of this errors?
This is my php Code.
<?php
// Put your device token here (without spaces):
$deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Put your private key's passphrase here:
$passphrase = 'xxxxxxxxxx';
// 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.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',
'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;
}
// Close the connection to the server
fclose($fp);
?>
Please help me to fix this errors.
I have seen that you didn't use any SSL Certificate. Create you SSL Certificate for your app first.
I had same problem and it've been solved.
Make sure that your $passphrase is right. $passphrase is password of your .pem file
If possible, you should create your .pem again.
Good luck!
I'm Ravi Malviya.
Code snippets.
//*//Notification Configration.(Can Only Test On Device)
1.PushChatKey.certSigningRequest //Computer authentication certificate then from keychain export PushChatKey.p12
2.Explicit App ID //create it With Enable push notifcation.
3.aps_development.cer //create it under app id and then download it.
4.Create developer certificate and provisioning profile on This App id
$ openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem / /convert Apple Development iOS Push Services to .pem (public key)
$ openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12 // convert PushChatKey.p12 to .pem (public key)
$ cat PushChatCert.pem PushChatKey.pem > ck.pem //concate pub and pri key //RSA
$ telnet gateway.sandbox.push.apple.com 2195 //check properly APNS working or not
$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem //Enter pass phrase for PushChatKey.pem:
//Web server side Configration.
1.sudo su - (create root login from login options->network Account server (click join)->open directory utility ->Edit->Enable root)
2.Start Apache
# apachectl start
3.Enter in browser http://localhost
4.Enable Php Source can get under this directory
# cd /etc/apache2/
# cp httpd.conf httpd.conf.bak
5.open httpd.conf in textMate
vi httpd.conf
6.remove # and save file from
LoadModule php5_module libexec/apache2/libphp5.so
7.Restart your apache
# apachectl restart
Varify php enable or not place php file in /Library/WebServer/Documents
Open url: http://localhost/fileName.php
//////From Command line
cd Documents/RAndD/ZObjectiveC/
php simplepush.php