Easy fix IOS remote message push _ios

Source: Internet
Author: User
Tags app service key string openssl openssl x509 php server


First, the introduction
There are two ways to push messages in iOS, local push and remote push, local push in http://www.jb51.net/article/93602.htm this blog has a detailed introduction, here mainly discusses the remote push process and configuration process.



Second, the principle of remote push mechanism



1, from a very fire in the picture
Search iOS remote push, you can always see a map of the following process, because this picture is really hot, so I also quote it here:






The picture is clear, which is roughly what it means: Your application server sends messages to Apple's APNs servers, the APNS server pushes messages to the designated iphone, and finally the iphone is responsible for pushing messages to your app. Let's not say how the process is implemented, just looking at the process, you might think that adding an apple APNs between your server and client is not a burden on developers. In fact, the result is quite the opposite, because Apple's unified management of push makes our developers ' work a little easier.



2, how to connect the server to the client



If you are doing Android development, you must be very knowledgeable about long links and heartbeat packs. In fact, most of the push for Android apps is actually done through long links. Because of the openness of Android, apps are easy to start and back-end long links, and heartbeat verification is always guaranteed that long links belong to the state, and then the server directly push messages. If the iOS developer is using this kind of thinking, it's very difficult to keep an app service in iOS and not be killed by the system, I can only say it's too difficult. Through the flow chart above, we can easily understand that there is always a long link in iOS, that is, the system itself, this long link is always connected to the APNs server, and then unified management of all applications push.



3, this is the advantages of iOS push mechanism?



The following are just some of my personal thoughts. The system is not good or bad, the pros and cons of personal preferences.



1, because push the service end is the AppleID authentication user, the push sends the reliability to be high.



2, all push message by APNs Unified Management, high efficiency.



3, in the client only system maintenance a long link, saving user flow consumption and mobile phone performance consumption, and improve security, so that there is malicious push and rogue software to reduce the chance.



Three minutes to get your app to receive a remote push



1, If you want to do something good, you must first sharpen it.--Create push certificate
(1) Request CSR files



Find the keychain access in the MAC application and open it.



Click on the key string in the options bar to access the certificate Assistant in:






Select to request a certificate from a certification authority:






Fill in the email and name, select Save to disk, then continue.



At this time, we store the place with such a file: Certificatesigningrequest.certsigningrequest.



(2) Export key file



Open the keychain, you will find a pair of keys, the name is the above you fill in the common name.



We select a private key for export and then set our own password:






At this point we have a file with a suffix named. P12.



(3) Create AppID



To Https://developer.apple.com's member Center:






After landing with your paid developer AppleID, select certificates:









If your project has created the app ID, you may not have to re-create it, but the app ID you created must support remote push. If it has not yet been created, click the plus sign to create a:






There are two types of app IDs in the following interface: explicit and wildcard, respectively special and wildcard, we need push function, this ID can't be a wildcard, so we choose the first one.






The bundle ID that needs to be filled in here must be the same as in our app:






In the app ID's service settings, push notification is checked and clicked continue.
I'll get you a minute. iOS remote message push



Then click Submit, and then click Done. The app IDs we just created will appear in our list of app IDs.



(4) Create a certificate



Click the app ID we just created, and you'll see that the push notification one behavior is not set. We click Edit.






In the push notifications settings is the following interface, development is the development of certificates, production is the product certificate, we now need to test, so with development certificate, on line to use the production certificate. Click Create Certificate.






Then click Continue, the following interface will let us select a CSR file, the first step we created the file here to come in handy, select that file, click Generate.






To download the created certificate to your computer:






So far, we have three files, namely CSR files,. p12 files,. cer files. To put the three files in the same directory, the. cer file is divided into test and product two, and which choice is required. Write so much, our preparation can be regarded as finished, do not lose heart, in fact, your push work is basically finished. The application process is just a bit cumbersome, but the code for the project, we hardly have to configure.



2, the troops do not move, forage first--the server to carry out the information push settings



(1) Processing certificate



Open the terminal CD to the directory where we got the three files.



Execute the following command at the terminal:



$ OpenSSL x509-in aps_development.cer-inform der-out Pushcert.pem



Aps_development.cer is the file name of the. cer file that you just generated. A PEM file will be generated in the current folder, which is the corresponding certificate for our server.



Then execute the following command:



$ OpenSSL pkcs12-nocerts-out pushkey.pem-in KEY.P12



KEY.P12 is the file name of the. p12 file generated above. At this point the terminal will allow the input password, where the password is the key we set above the password. Enter the password and return, if the correct password, will let us enter a new password (must remember), enter two times, the terminal will prompt the successful creation of the Pushkey.pem file.



In the final step, we will generate two PEM files and become one:



$ cat Pushcert.pem Pushkey.pem > Ck.pem



(2) test whether the certificate is available



Execute the following command at the terminal:



$ telnet gateway.sandbox.push.apple.com 2195



Wait a little while, if the terminal shows the following situation, the certificate is OK.






Then execute the following command:



OpenSSL s_client-connect Gateway.sandbox.push.apple.com:2195-cert Pushchatcert.pem-key PushKey.pem
When you enter a password, carriage return displays the following results for successful connection:






3, the ends of the earth, one step away--configuration in the application



Add the following code to the appdelegate of our project:


 

The following is the code for the Web search PHP server:


<?php//Here to fill in the equipment token code $deviceToken = ' 74314cc9e8f747e2fa96c2c1585c830cdf994de6b453ce9fa1c09ba396b2f9e9 ';
Here is the key cipher $passphrase = ' abcabc ';

Push message $message = ' This is a push message ';
$ctx = Stream_context_create (); Stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', ' Ck.pem ');//ck file Stream_context_set_option ($ctx, ' SSL ', '

Passphrase ', $passphrase); Open a connection to the APNs server $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 ' => $message, ' sound ' => ' default ');

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);

 ?>


Put the above PHP file in the same directory as our CK file. In the current directory of the terminal, execute the following command:



$php push.php



If our device is Wang Lue Normal, we can receive a push message:






Iv. Points of attention



1, if the terminal to send information when the key is not accessible, such as error, please check whether the CD to the current directory, if there are problems, the key part of the new generation.



2, note that the characters in the PHP code is English character.



This article has been organized into the "iOS push Tutorial", welcome to learn to read.



The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.


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.