IOS Message Push Implementation APNS

Source: Internet
Author: User

This article just records how to configure the APNs push environment on your own computer, other such as the principle of push, the process of what is not written here.

One. Go to the Apple Developer Center and create an app ID. Note The app ID cannot be used with wildcard characters. and note the addition of the push Notification Service

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

Two. Create certificates and profiles for development and production.

Step slightly.

Attention

1. When creating profile, the app ID and certification should be correct. The profile you have created can also be edited again to modify its certificate and devices. You only need to go to Xcode = References and Accounts to refresh on the change.

2. When creating a certificate, we will use keychain to create a. certsigningrequest file on your computer, save this file, because if you do not need to update the file after the certificate expires, And with a new. certsigningrequest file, the certificate that the server needs to use will need to be rebuilt as follows.

Three. Create a certificate for the server to use

1. Export the private Key of the corresponding certificate in keychain. (Easy to use later, remember 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:

$ 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 above feedback, it means that your MAC can go to
Up to APNs. Press CTRL + C to close the connection. If you get an error message, then you need to make sure that 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 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 disconnects. If there is a problem establishing the connection, OpenSSL will give you an error message,
The Ck.pem file is the file that we need to get the Push server connection APNS.

Five. Configure the local server

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

Now Apache's home directory is/libary/webserver/documents/, you can put the file test 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 the #loadModule in/etc/apache2/httpd.conf php5_module libexec/apache2/libphp5.so to

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

After that, you can modify the php.ini configuration according to your own habits.  

for example, error_reporting = E_all & ~e_notice modified to

error_reporting = E_all

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

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

4. Create the push.php file and copy it to the/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 on-line version of the different.        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 '    Say reader ', ' title ' = ' Xiaoxiaoshui ', ' url ' = ' http://www.apple.com.cn ');    $message = Array (' msg ' = = ' go to Product detail page ', ' itemtype ' = ' 2 ', ' id ' = ' 192172 '); $message = Array (' msg ' = ' Go to menu Page ', ' itemtype ' = ' 1 ', ' zktype ' = ' 1 ', ' order ' = ' 1 ', ' zksubtype ' = ' 1 ', '    Zktitle ' + ' 9.9 pack 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 exactly the release address//$fp = Stream_socket_client (' ssl://gateway.push.apple.com:        2195 ', $err, $errstr, Stream_client_connect, $ctx); This is the sandbox test address, after posting to AppStore 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 ' and ' 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 after it is run on the real machine.

Restart Apache, sudo apachectl restart

  This way we will receive a notification when we visit a http://localhost/push/push.php.

Reference:

PHP Environment: http://ccvita.com/398.html apns:http://www.cnblogs.com/wengzilin/archive/2012/05/02/2479241.htmlhttp:// 1679554191.iteye.com/blog/1717531http://www.cnblogs.com/iphone520/archive/2013/04/16/3023687.htmlhttp:// 1679554191.iteye.com/blog/1740055

IOS Message Push Implementation APNS

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.