How to configure the APNs push environment on your own computer _ios

Source: Internet
Author: User
Tags openssl openssl x509 pack strlen what openssl ssl certificate

This article is just a record of how to configure the APNs push environment on their own computer, other such as the principle of push, process or something here will not write.

A. Go to the Apple Developer Center and create the app ID. Note The app ID cannot use wildcard characters. And note that the push Notification Service is added

For an already created app ID, you can also edit it to add a push Notification Service

Two. Create the certificates and profiles of development and production.

Step slightly.

Attention

1. The app ID and certification should be correct when creating profile. For profiles that have already been created, you can edit them again to modify their certificates and devices. After the modification, you only need to go to Xcode => References => accounts to refresh.

2. When creating a certificate, we will use keychain to create a. certsigningrequest file on the computer first, this file please save, because if you do not use this file to update after the certificate expires, With a new. certsigningrequest file, the certificate that the server needs to use requires the following steps to regenerate.

Three. Create a certificate to use with the server

1. In keychain, export the private Key of the corresponding certificate. (Easy to use later, recorded as 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

four. To test whether the certificate is working, execute the following command :

Copy Code code as follows:
$ 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, it means your MAC will be able to
Reach APNs. Press CTRL + C to close the connection. If you get an error message, then you need to make sure that your firewall allows 2195 ports.

Then connect again, this time using our SSL certificate and private key to set up a secure connection:

Copy Code code as follows:
$ OpenSSL s_client-connect Gateway.sandbox.push.apple.com:2195-cert Pushchatcert.pem-key PushChatKey.pem

Enter Pass phrase for PUSHCHATKEY.PEM:


You will see a complete output that lets you understand what OpenSSL is doing in the background. If the connection is successful, you can type some characters.
When you press ENTER, the service is disconnected. If there is a problem setting up the 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.

Five. Configure the local server

1. Enable Apache
Mac OS X 10.5.6 with Apache 2.2.9, run directly on the command line Apachectl Start,apache.

Now Apache's home directory is/libary/webserver/documents/, you can put file test in this directory.

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

Modify the #loadModule in/etc/apache2/httpd.conf php5_module libexec/apache2/libphp5.so is

LoadModule Php5_module libexec/apache2/libphp5.so

Then copy the/etc/php.ini.default to/etc/php.ini.

Cp/etc/php.ini.default/etc/php.ini

Then you can modify the php.ini configuration according to your own habits.

For example, change the error_reporting = E_all & ~e_notice to

error_reporting = E_all

Finally, restart Apache, you can set up a phpinfo.php in the/libary/webserver/documents/directory to test.
sudo apachectl restart

3. Copy the CK.PEM generated by step four to/library/webserver/documents/

4. Create push.php files and copy them to/libary/webserver/documents/

<?php//Here is the devicetoken we get above, copy it directly (remember to remove the space)//devicetoken in the beta version and the online version of the different.
  Lei ipod Touch $deviceToken = ' f5b70734ea5f4b91c904544f75457d6ecb908488317e097abaab769e741e6752 ';
  Put your private key ' s passphrase here: $passphrase = ' 1111 ';
  Put your alert: $message = ' I-i-push test! '; $message = Array (' msg ' => ')
  Xiaoxiaoshui reader ', ' title ' => ' Xiaoxiaoshui ', ' url ' => ' http://www.apple.com.cn ');
  $message = Array (' msg ' => ' Go to Commodity detail page ', ' itemtype ' => ' 2 ', ' id ' => ' 192172 '); $message = Array (' msg ' => ' Go to Menu Page ', ' itemtype ' => ' 1 ', ' Zktype ' => ' 1 ', ' Order ' => ' 1 ', ' Zksubtype ' => ' 1 ', '
  Zktitle ' => ' 9.9 package mail ');
  $message = Array (' msg ' => ' software upgrades ');
  $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 exactly the releaseAddress//$fp = stream_socket_client (' ssl://gateway.push.apple.com:2195 ', $err, $errstr, Stream_client_connect, $ctx); This is the sandbox test address, released to the App Store after you remember to modify oh $fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $err, $errstr , 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 ' => ' tease you to play!) 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 is not delivered '.
  Php_eol; else ECHO ' message successfully delivered ' .
  Php_eol;
  Close the connection to the server fclose ($FP);
 ?>

Note: The Devicetoken in your code needs to be copied and replaced after it has been run on the real machine.

Restart Apache, sudo apachectl restart

So when we visit http://localhost/push/push.php, we will receive a notice.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.