How do I configure the APNS push environment on my computer

  • 2020-05-13 03:24:17
  • OfStack

This article only records how to configure the APNS push environment on your own computer. The rest, such as the principle and process of push, will not be written here.

1. Go to the Apple developer center and create App ID. Note that App ID cannot use wildcards. And be careful to add Push Notification Service

For APP ID that has been created, you can also edit to add Push Notification Service to it

2. Create Certificates and Profiles for development and production.

Step a little.

Pay attention to

1. When creating Profile, App ID and Certification should be correct. For the Profile that has already been created, you can edit and modify its certificate and Devices again. After modification, you only need to go to Xcode = > References = > Accounts, Refresh is fine.

2. When creating the certificate, we will use KeyChain to create a.certSigningRequest file on the computer. Please save this file, because if a new.certSigningRequest file is used instead of this file after the certificate expires, then the certificate needed by the server will need to be regenerated according to the following steps.

3. Create a certificate for the server to use

1. Export Private Key of the corresponding certificate in KeyChain. (for later use, Push.p12)

2. openssl x509-in aps_developer_identity. cer-inform der-out PushChatCert. pem

3. openssl pkcs12-nocerts-out PushChatKey.pem-in Push.p12

4. cat PushChatCert. pem PushChatKey pem > ck.pem

4. To test whether the certificate works, execute the following command:

$ telnet gateway.sandbox.push.apple.com 2195
  Trying 17.172.232.226...
  Connected to gateway.sandbox.push-apple.com.akadns.net.
  Escape character is ' ^]'.

It will attempt to send a regular, unencrypted connection to the APNS service. If you see the feedback above, then your MAC can reach you
APNS. Press Ctrl+C to close the connection. If you get an error message, 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:

$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem
  Enter pass phrase for PushChatKey.pem:

You'll see a full output that gives you an idea of what OpenSSL is doing in the background. If the connection is successful, you can type 1 characters.
When you press enter, the service disconnects. If you have problems establishing a connection,OpenSSL will give you an error message,
The ck.pem file is the file we need to get the Push server to connect to APNS.

5. Configure the local server

1. Enable Apache
Mac OS X 10.5.6 comes with Apache 2.2.9 and runs apachectl start directly from the command line. Apache is done.

Now Apache home directory is/Libary/WebServer Documents /, you can place files in this directory test.

2. Enable PHP
Mac OS X 10.5.6 comes with PHP 5.2.6. What we need to do is to add PHP to Apache.

Modify/etc/apache2 / httpd conf in # loadModule php5_module libexec/apache2 / libphp5 so for

loadModule php5_module libexec apache2 / libphp5 so

Then/etc php. ini. default copy for/etc/php ini.

cp etc/php. ini. default/etc/php ini

Then you can modify the configuration of php.ini as you like

For example, error_reporting = E_ALL & ~E_NOTICE is changed to

error_reporting = E_ALL

Finally, restart Apache, can be in/Libary/WebServer/Documents/directory to establish a phpinfo. php to test.
sudo apachectl restart

3. The step 4 generated ck. pem copied to/Library/WebServer/Documents /

4. Create push. php file and copied to/Libary/WebServer Documents /


<?php
  //  This is what we got up here deviceToken , copy it directly (remember to remove the space) 
//deviceToken  Not on the test version and the live version 1 The sample.   
  //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'=>' Novel reader ','title'=>' affair ','url'=>'http://www.apple.com.cn');
  //$message = array('msg'=>' Go to the product details page ','itemtype'=>'2','id'=>'192172');
  //$message = array('msg'=>' Go to the menu page ','itemtype'=>'1','zktype'=>'1','order'=>'1','zksubtype'=>'1','zktitle'=>'9.9 Package mail ');
  //$message = array('msg'=>' Software upgrade ');
  $ctx = stream_context_create();
  stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
  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://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  // This is the sandbox test address, published to appstore Remember to revise it later 
  $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' => ' Play with you! Ha ha. ',
             'sound' => 'beep.wav',
             '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 copied and replaced when it is up and running on the real machine.

Restart Apache, sudo apachectl restart

So that when we visit one http: / / localhost push/push php will receive a notification.


Related articles: