Sending iOS notification through a php script: 

2019-02-14 00:04发布

问题:

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 = '';

回答1:

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.



回答2:

Setting $passphrase = '1234' to $passphrase = "1234" fixed the issue for me.



回答3:

If the file ck.pem does exist in that location it may be that the php script does not have access to it. I would change the permissions of the folder/files to something more lenient or try running the script as the superuser:

sudo php simplepush.php

Edit 1:

After some research it looks like the pem file could be in an unexpected format. You could try changing the order of the certificates that were combined in the file. You can also try using separate files and specifying each file using the 'stream_context_set_option' functions.

Edit 2:

If you can, try to set up the passwords for your production files the same way as the tutorial. It's possible the passphrase is required for it to work. Again, sounds like something is wrong with the pem file, either generated incorrectly, missing, or in the wrong format. Check the file. Maybe even post it up in your question if it is not a security risk. You can probably just regenerate a new key after figuring out what is wrong.



回答4:

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



回答5:

I got the same error.

I'm sure my pem file is right, i follow this guide: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ProvisioningDevelopment.html

debug a lot and i figured out i forget the passphrase.

PS: I used this php script: https://github.com/duccio/ApnsPHP, maybe it helps you.



回答6:

I have encountered the same problem, I am using 1234 as passphrase. Instead of writing $passphrase = '1234'; I have written as $passphrase = 1234; and it is working fine



回答7:

Always make sure you don't use old APNS codes (e.g. you are using the same database table for your development and production application). This causes the Apple APNS service to disconnect because it received an invalid APNS code, as a different APNS code is used for development and the actual published app.

Another remark: do not forget to regenerate the key pair if you publish your app to the appstore. A development certificate won't work in production!