Implementation of simple push notifications (push Notification) on iOS

Source: Internet
Author: User
Tags php server vars ssl certificate
<span id="Label3"></p><p><p>http://blog.csdn.net/daydreamingboy/article/details/7977098</p></p><p><p></p></p>Implementation of simple push notifications (push Notification) on iOS<p><p></p></p><p align="left"><p align="left">According to this very good tutorial (http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12), combine your own practice, write down a little note, For reference only:)</p></p><p><p>Due to the length of the space, I list a simple directory, as follows</p></p><p><p>1) Understanding the Apple push notification mechanism</p></p><p><p>2) Create App ID</p></p><p><p>3) Create a CSR file</p></p><p><p>4) Create Provisioning Profile</p></p><p><p>5) get device Token in Xcode project</p></p><p><p>6) Create A. Pem file</p></p><p><p>7) Write PHP server code, send notifications</p></p>1. Understanding the Apple push notification mechanism<p><p></p></p><p align="left"><p align="left"></p></p><p><p>From the flowchart above, you can see an app that can receive push notifications, which requires 3 things:</p></p><p><p></p></p> <ul> <ul> <li>App ID (uniquely identifies an app Program)</li> <li>Provisioning profile (app release requires it, so push notifications can only be tested on the real machine)</li> <li>Device Token (which is unique to the push notification Feature)</li> </ul> </ul><p><p></p></p><p><p>The server side that can push notifications is 2 things:</p></p><p><p></p></p> <ul> <ul> <li>SSL Certificate</li> <li>Private Key</li> </ul> </ul><p><p></p></p><p><p>(because I am not clear about the information encryption, so here does not explain)</p></p><p><p></p></p><p><p>It is important to note that the APNs (applepush Notification Service) Server completes the function of sending device token and notification content, and the 2 actions are passive, that is, the first action was initiated by the app, The second is the server that initiated the push Notification.</p></p><p><p>For me, there is so much to Understand. Below I follow the reference article for the Experiment.</p></p><p><p></p></p>2. Create app ID<p><p></p></p><p align="left"><p align="left">After clicking on the "New App ID" button, the following</p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left">Description content can be arbitrary, bundle Identifier (app ID Suffix) must and the bundle Identifier when creating the app project, as follows</p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left">Click "Submit", click on the left navigation "app ids" to find the newly created app ID, as follows</p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left"></p></p><p><p>After clicking "Configure", the following</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>Tick "Enable for Apple Push Notification service" and then click the Red "Configure" button, where you only get the certificate for development temporarily. A dialog box POPs up, as follows</p></p><p><p></p></p><p align="left"><p align="left">After clicking "Continue", we will upload a CSR file as follows</p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left"></p></p><p><p>Use the Keychain Access (keychainaccess) application below to create the required CSR file (. certsigningrequest File)</p></p><p><p></p></p>3. Create a CSR file<p><p>Keychain access is located in The/applications/utilities directory, open it as follows</p></p><p><p></p></p><p align="left"><p align="left">Then the popup window is as Follows.</p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left">UserEmail address Feel free to write, Common name is the same, note tick "Save to disks", and then click "Continue". Quickly generate the required files to find it.</p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left">Go back to the page below and upload the Helloremotenotification.certsigningrequest file that you just created with Keychainaccess.</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>Soon the required certificate will be ok, as follows</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>Click "Continue" and then click "done".</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>Found the status above is enabled, and more "Download" button, click on it, Download a file called "aps_development.cer". Double-click to open it,</p></p><p><p></p></p><p align="left"><p align="left">Locate the private key named "helloremotenotification" in the "Keys" column (note that private key, not public key), Right-click on it and select "Export" Helloremotenotification "..." to export A. P12 file (requires a password) as follows (3 files in Total)</p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left"></p></p><p><p>The following starts with The. P12 file that you just created, creating a profile provision file</p></p><p><p></p></p>4. Create a Provisioningprofile file<p><p></p></p><p align="left"><p align="left"></p></p><p><p>In the, click on the "New profile" button, as follows</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>Fill in "profile Name", tick "Certificate", "App id" to select the correct ID that we created previously, i.e. pushnotification; the last association needs to test the real device. Click "Submit", as follows</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>Can see more than one provisioning profile file, Click the "Download" button to download it, then we produced a total of 4 files, as follows</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>Double-click the "pushnotification.mobileprovision" file, or drag it into Xcode.</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>In xcode, Locate the code signing entry, for example, to drag a debug configuration powertrain into the provisioning profile corresponding iphone Developer.</p></p><p><p></p></p>5. Get device Token in Xcode project<p><p></p></p><p align="left"><p align="left"></p></p><p><p>In the Application:didfinishlaunchingwithoptions: method, register to use remote Notification.</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>Add 2 methods, application:didregisterforremotenotificationswithdevicetoken: and Application: Didfailtoregisterforremotenotificationswitherror: used to obtain device token and print Errors. Run our built helloremotenotification project, if the above steps are correct, you should print out device Token, as follows</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>There may also be errors such as</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p></p></p>6. Create A. Pem file<p><p></p></p> <ul> <ul> <li>Convert an existing. cer file to A. Pem file</li> <li>Convert existing. p12 files to. Pem file (password required)</li> <li>finally, Merge the above 2. Pem files into 1. PEM files (you need to enter a new Password)</li> </ul> </ul><p><p></p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left">Aps_development.cer->helloremotenotification.pem (renamed as Helloremotenotificationcert.pem Below)</p></p><p><p>Helloremotenotification.p12-> Helloremotenotificationkey.pem</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>Helloremotenotification.pem +helloremotenotificationkey.pem merged into CK2.PEM</p></p><p><p></p></p>7, Write PHP server code, send notifications<strong><strong>[php]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <li class="alt"><li class="alt"><?php</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">Put your device token here (without spaces):</span></li></li> <li><li><span class="vars">$deviceToken = <span class="string">' Device token> ' of the <xcode console output; </span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">Put your private key ' s passphrase here:</span></li></li> <li class="alt"><li class="alt"><span class="vars">$passphrase = <span class="string">' < last password entered > '; </span></span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">Put your alert message here:</span></li></li> <li><li><span class="vars">$message = <span class="string">' My first push notification! '; </span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">////////////////////////////////////////////////////////////////////////////////</span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="vars">$ctx = Stream_context_create ();</span></li></li> <li class="alt"><li class="alt">Stream_context_set_option (<span class="vars">$ctx, <span class="string">' SSL ', <span class="string">' Local_cert ', <span class="string">' Ck2.pem '); </span></span></span></span></li></li> <li><li>Stream_context_set_option (<span class="vars">$ctx, <span class="string">' SSL ', <span class="string">' passphrase ', <span class="vars">$passphrase); </span></span></span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">Open a connection to the APNS server</span></li></li> <li class="alt"><li class="alt"><span class="vars">$fp = Stream_socket_client (</span></li></li> <li><li><span class="string">' ssl://gateway.sandbox.push.apple.com:2195 ', <span class="vars">$err,</span> </span></li></li> <li class="alt"><li class="alt"><span class="vars">$errstr, stream_client_connect|.  stream_client_persistent, <span class="vars">$ctx); </span></span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">If (! <span class="vars"> $fp)</span></span></li></li> <li><li><span class="func">exit (<span class="string">"Failed to Connect: $err $errstr".  php_eol); </span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="func">Echo <span class="string">' Connected to APNS '.  php_eol; </span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">Create the payload body</span></li></li> <li class="alt"><li class="alt"><span class="vars">$body [<span class="string">' aps '] = <span class="keyword">Array (</span> </span></span></li></li> <li><li><span class="string">' alert ' = <span class="vars">$message,</span> </span></li></li> <li class="alt"><li class="alt"><span class="string">' sound ' = ' <span class="string">default '</span> </span></li></li> <li><li>);</li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">Encode the payload as JSON</span></li></li> <li class="alt"><li class="alt"><span class="vars">$payload = Json_encode (<span class="vars">$body); </span></span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">Build the binary notification</span></li></li> <li><li><span class="vars">$msg = CHR (0). pack (' n ', ' + '). pack (' <span class="func"><span class="string"><span class="string">h* ', <span class="vars">$deviceToken). Pack (<span class="string">' n ', <span class="func">strlen (<span class="vars">$payload)).  <span class="vars">$payload; </span></span></span></span></span></span></span></span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">Send it to the server</span></li></li> <li class="alt"><li class="alt"><span class="vars">$result = fwrite (<span class="vars">$fp, <span class="vars">$msg, <span class="func">strlen (<span class="vars">$msg)); </span></span></span></span></span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">If (! <span class="vars"> $result)</span></span></li></li> <li><li><span class="func">echo <span class="string">' Message not delivered '.  php_eol; </span></span></li></li> <li class="alt"><li class="alt"><span class="keyword">Else</span></li></li> <li><li><span class="func">Echo <span class="string">' Message successfully delivered '.  php_eol; </span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">Close the connection to the server</span></li></li> <li class="alt"><li class="alt">Fclose (<span class="vars">$fp); </span></li></li> </ol><p><p>Note: Modify the following two lines of code</p></p><p align="left"><p align="left"></p></p><strong><strong>[php]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li class="alt"><span class="vars">$deviceToken = <span class="string">' Device token> ' of the <xcode console output; </span></span></li> <li></li> <li class="alt"><span class="comment">Put your private key ' s passphrase here:</span></li> <li><span class="vars">$passphrase = <span class="string">' < last password entered > '; </span></span></li> <li class="alt">......</li> <li>......</li> <li class="alt">Stream_context_set_option (<span class="vars">$ctx, <span class="string">' SSL ', <span class="string">' Local_cert ', <span class="string">' Ck2.pem '); </span></span></span></span></li> </ol> </ol><p><p></p></p><p><p>Run the above PHP script, as follows</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p>You can see the push notifications received on the ipad, as below, to show that the practice is successful!</p></p><p><p></p></p><p align="left"><p align="left"></p></p><p><p></p></p><p><p>Resources:</p></p><p><p>1, http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12</p></p><p><p>Implementation of simple push notifications (push Notification) on iOS</p></p></span>
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.