Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending iOS notification through a php script: 'Unable to set private key file'

I have used the php script from the well known Ray Wanderlich tutorial to send push notifications during development phase. They were triggered properly after I created the pem file (from the p12 and aps_development.cer file) and mentioned the device token in the php script.

However, when I am using a pem file which has production p12 and aps_production.cer file, the notification is not even reaching the APNS server. It is showing the below error at the local server end itself.

Unable to set private key file `/Users/administrator/Desktop/SimplePush/ck.pem' in /Users/administrator/Desktop/SimplePush/simplepush.php on line 22

Warning: stream_socket_client(): failed to create an SSL handle in /Users/administrator/Desktop/SimplePush/simplepush.php on line 22

Warning: stream_socket_client(): Failed to enable crypto in /Users/administrator/Desktop/SimplePush/simplepush.php on line 22

Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error) in /Users/administrator/Desktop/SimplePush/simplepush.php on line 22

What could be the reason for it. The p12 file does not have any password (though I have configured a password for the pem file) and this is how it has been given in the php script.

// Put your private key's passphrase here:
$passphrase = '';
like image 580
OutOnAWeekend Avatar asked Jan 09 '14 07:01

OutOnAWeekend


People also ask

What is PHP web push notification?

PHPZAG Team PHP Web Push Notification is a feature to send customized clickable message to display in subscribed user’s web browsers like Chrome, Firefox, Safari etc. The push notifications are useful to update user’s with specific news, chat, new email and time bound information like offers etc.

How to get user’s notification details using Ajax request in PHP?

In notification.php file, we will get logged in user’s notification details and returned as json response as this file called by Ajax request. $sqlQuery = "SELECT * FROM ".$this->notifTable." WHERE username='$user' AND notif_loop > 0 AND notif_time <= CURRENT_TIMESTAMP ()"; return $this->getData ($sqlQuery);

How to execute notification on every 20 seconds using Ajax request?

In notification.js file, we will create function showNotification () to make Ajax request to notification.php to get notification details for logged in user and execute notification. Then we will call function showNotification () to execute notification on every 20 seconds.


2 Answers

It was not the correct p12 file. I exported the correct p12 file from Keychain and it went well.

Lessons learnt -

  1. If the p12 and cer files you are using do not correspond to each other, obviously there wouldn't be any error shown during the concatenated pem file creation. But when you execute the script, there will be an error shown.

  2. The above error messages do not necessarily imply that the p12 file's passphrase is incorrect. They may also mean that the p12 file does not correspond to the used cer file.

like image 142
OutOnAWeekend Avatar answered Sep 28 '22 14:09

OutOnAWeekend


if you running the file from the command line maybe try to give the full path to the ck.pem file

change the line : stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');

to

stream_context_set_option($ctx, 'ssl', 'local_cert', '/path/to/your/file/ck.pem');

work for me

like image 23
ron Avatar answered Sep 28 '22 14:09

ron