This article only records how to configure the APNS push environment on your computer. Other principles, processes, etc. will not be written here.
1. Go to the Apple Developer Center and create an App ID.Note that wildcard characters cannot be used for App ID. And pay attention to adding Push Notification Service
For the APP ID that has been created, you can also edit it and add Push Notification Service to it.
2. Create Certificates and Profiles for development and production.
The steps are omitted.
Notice
1. When creating a profile, the App ID and Certification must be correct. For the already created profile, you can also edit and modify its certificates and devices again. After modification, you only need to go to Xcode => References => Accounts Refresh.
2. When creating a certificate, we will use KeyChain to create a .certSigningRequest file on the computer first. Please save this file, because if you do not use this file to update after the certificate expires, but use a new .certSigningRequest file, the certificate that the server needs to use needs to be regenerated according to the following steps.
3. Create a certificate for the server to use
1. Export the Private Key of the corresponding certificate in KeyChain. (Easy to use later, it is marked as Push.p12)
2. openssl x509 -in aps_developer_identity.cer -inform der -out
3. openssl pkcs12 -nocerts -out -in Push.p12
4. cat >
4. To test whether the certificate works, execute the following command:
Trying 17.172.232.226...
Connected to .
Escape character is ‘^]'.
It will try to send a regular, unencrypted connection to the APNS service. If you see the feedback above, it means your MAC can reach
APNS. Press Ctrl+C to close the connection. If you get an error message, then you need to make sure your firewall allows port 2195.
Then connect again, this time using our SSL certificate and private key to set up a secure connection:
Enter pass phrase for :
You will see a complete output that lets you understand what OpenSSL does in the background. If the connection is successful, you can type some characters.
When you press Enter, the service will be disconnected. If there is any problem when establishing the connection, OpenSSL will give you an error message.
Files are the files we need to get the Push server to connect to APNS.
V. Configure the local server
1. Enable Apache
Mac OS X 10.5.6 comes with Apache 2.2.9. Just run apachectl start on the command line, and Apache will do it.
Now Apache's home directory is /Libary/WebServer/Documents/, you can place file tests in this directory.
2. Enable PHP
Mac OS X 10.5.6 comes with PHP 5.2.6, and all we need to do is add PHP to Apache.
Modify #loadModule php5_module libexec/apache2/ in /etc/apache2/ to
loadModule php5_module libexec/apache2/
Then copy /etc/ to /etc/.
cp /etc/ /etc/
Then you can modify the configuration according to your own habits
For example, modify error_reporting = E_ALL & ~E_NOTICE to
error_reporting = E_ALL
Finally, restart Apache and you can create a test in the /Libary/WebServer/Documents/ directory.
sudo apachectl restart
3. Copy the generated copy of step 4 to /Library/WebServer/Documents/
4. Create a file and copy it to /Libary/WebServer/Documents/
<?php // Here is the deviceToken we got above, copy it directly (remember to remove the spaces)//DeviceToken is different in the test version and the online version. //lei ipod touch $deviceToken = 'f5b70734ea5f4b91c904544f75457d6ecb908488317e097abaab769e741e6752'; // Put your private key's passphrase here: $passphrase = '1111'; // Put your alert message here: $message = 'My first push test!'; //////////////////////////////////////////////////////////////////////////////// $message = array('msg'=>'Small novel reader','title'=>'Small novel','url'=>''); //$message = array('msg'=>'Go to the product details page','itemtype'=>'2','id'=>'192172'); //$message = array('msg'=>'go to menu page','itemtype'=>'1','zktype'=>'1','order'=>'1','zksubtype'=>'1','zktitle'=>'9.9 free shipping'); //$message = array('msg'=>'Software Upgrade'); $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', ''); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); var_dump($ctx); // Open a connection to the APNS server //This is the exact publishing address //$fp = stream_socket_client('ssl://:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); //This is the sandbox test address. Remember to modify it after publishing it to the appstore $fp = stream_socket_client('ssl://: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' => 'Can you play! Ha ha. ', 'sound' => '', 'badge' => 1 ); $body['type']=2; $body['data']=$message; // 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); ?>
Note: The DeviceToken in the code needs to be run on the real machine and copied and replaced.
Restart Apache, sudo apachectl restart
This way when we visithttp://localhost/push/A notification will be received.