iOS Push Summary (certificate generation, client development, server development)

Source: Internet
Author: User
Tags pkcs12 ssl certificate ssl connection

1. Introduction to the push process

1.1, the app starts the process, uses the Uiapplication::registerforremotenotificationtypes function to communicate with Apple's APNs server, issues the registration remote push request. If the registration succeeds, the callback function application: (uiapplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken will be triggered and the app can get Devicetoken, which is a device-related string.

1.2, the app obtains to the Devicetoken, sends the Devicetoken to own service side. 1.3, the service side to get Devicetoken, using the certificate file, to Apple's APNs server to initiate an SSL connection. After the connection is successful, send a JSON string containing the type and content of the push message. 1.4, the Apple APNs server gets the JSON string, sends the notification message to the app, makes the app's callback function application: (uiapplication *) application Didreceiveremotenotification: (Nsdictionary *) UserInfo is called, the app can get the content of the push message from the UserInfo.     2. The certificate file used and the build process2.1. certsigningrequest file, which is generated on the MAC system and is used to request a push certificate file on the Apple website. Build Process:Open the Keychain Access software in the application, select Keychain Access from the menu-"certificate Assistant"-"request a certificate from a certification authority", fill out the mailbox and name, and select Save to disk. You can generate a certificatesigningrequest.certsigningrequest file locally. 2.2. Register an app ID that supports push, which is used later. Build Process: Enter developer.apple.com, select Member Center-certificates, Identifiers & Profiles-identifiers-app Ids, then select Register APP ID, set the AppID name, and the app ID suffix column must select the explicit app ID, then set the bundle ID, and finally tick the Push notifications in app services, This allows you to register a aphid that supports push.   2.3. Push the certificate CER file, which is generated in developer.apple.com to generate the files required by the server. Build Process:Enter developer.apple.com, select Member Center-certificates, Identifiers & profiles-certificates, then select Create Certificate, Types are divided into development and product. Take development For example, select Apple Push Notification service SSL (Sandbox), then next, select the previously generated support Push AppID, and then next, submit the CSR file that you created earlier, The next step is to generate a CER file and save it locally. 2.4. Build the certificate file used by the service side. If you are using the Mac version of the Pushmebaby tool on the Internet to send push messages on Mac machines, then the CER file above will suffice. If you are using PHP and java/c# to develop your own server, you will also need to convert the CER file above to generate a PEM file or a p12 file.          generate PHP with PEM file process for:        First double-clicking on the previously saved CER file opens the Keychain access software, which will appear with an apple Development  ios push Services Certificate, a public key and a private key, the name of the key is the same as the name that is filled in in the certificate assistant.       Check certificate, export as APNS-DEV-CERT.P12 file       Check private key, export to APNS-DEV-KEY.P12 file       Over terminal commands convert these files to PEM format:      OpenSSL pkcs12-clcerts-nokeys-out apns-dev-cert.pem-in apns-dev-cert.p12& nbsp     OpenSSL pkcs12-nocerts-out apns-dev-key.pem-in apns-dev-key.p12      Finally, you need to combine two PEM files into one AP Ns-dev.pem file, this file needs to be connected to APNs using:      cat Apns-dev-cert.pem Apns-dev-key-noenc.pem > Apns-dev.pem          Generate java/c# p12 file process for:       &NBSP;OPENSSL Pkcs12-clcerts-nokeys- Out apns-dev-cert.pem-in apns-dev-cert.p12      &NBSP;OPENSSL Pkcs12-nocerts-out apns-dev-key.pem-in AP ns-dev-key.p12      &NBSP;OPENSSL pkcs12-export-in Apns-dev-cert.pem-inkEY apns-dev-key.pem-certfile certificatesigningrequest.certsigningrequest-name "Push"-out push.p12     & nbsp;  2.5. Generate the provisioning file used by Xcode, which is used for real machine debugging. Build Process:Enter developer.apple.com, select Member Center-certificates, Identifiers & profiles-provisioning Profiles, and select Create Provisio Ning file, then select iOS App development, next select AppID, select the previously established support push AppID, then next select Support Push certificate, next tick the device ID you need to support, The final step is to set the file name of the provisioning file so that the provisioning file is generated. 3. Development of the service side 3.1, if you just want to test the push of the message on the Mac computer, you can use the Pushmebaby tool, it is relatively simple to use. The tool is open source, can be downloaded from Https://github.com/stefanhafeneger/PushMeBaby, the execution of the code is actually set up the SSL certificate, then connect APNs, and then send the JSON data. Because the SSL logic is being processed, the code is slightly more points. When using a tool, replace the CER file in your project resource with your own CER file, and then replace the Devicetoken in your code with the Devicetoken of your own device. 3.2, using PHP Development Server        since PHP has built-in SSL modules, the process of using PHP to connect APNS servers to send JSON is actually very simple, as the code below:  the file can be accessed through a browser on the server, You can also interpret execution by command line, code: $ php-f pusher.php        copy code <?php$devicetoken= ' own Devicetoken '; No space $body = Array ("APS" + = Array ("alert" + = ' message ', "badge" = + 2, "sound" = ' default '));  //push mode, contains content and sound $ $ctx = Stream_context_create ();  //If there is a problem finding the PEM path on the Windows Server, the path is modified to the following method://$pem = DirName (__file__). ‘/‘ . ' Apns-dev.pem ';//linux's server directly writes the path of the Pem stream_context_set_option ($ctx, "SSL", "Local_cert", "Apns-dev.pem"); $pass = " XXXXXX "; Stream_context_set_option ($ctx, ' SSL ', ' passphrase ', $pass);////There are two servers to choose from, if it's for development testing, Select the second sandbox server and use the dev PEM certificate, if it is officially released, use the PEM of product and choose the official server $fp = Stream_socket_client ("ssl:// gateway.push.apple.com:2195 ", $err, $errstr, Stream_client_connect, $ctx); $fp = Stream_socket_client (" ssl:// gateway.sandbox.push.apple.com:2195 ", $err, $errstr, Stream_client_connect, $ctx); if (! $fp)      { echo "FailEd to connect $err $errstrn "; return;} Print "Connection ok\n";  $payload = Json_encode ($body); $msg = chr (0). Pack ("n", 32). Pack ("h*", Str_replace (",", $deviceToken)). Pack ("n", strlen ($payload)). $payload;  echo "Sending message:". $payload. " \ n "; fwrite ($fp, $msg);  fclose ($FP);? > Copy Code     4. Client development      4.1, download the previously established CER file and provisioning file, double-click, import into Xcode, in the build setting Code signing column, select the names of these two files, This enables push-enabled apps to be deployed to the real machine.            4.2, processing push messages             The client's handling of push messages is divided into two scenarios:           one. In the event that the app is not running, the system receives a push message and the user clicks the push message to launch the app. At this point, the          didreceiveremotenotification functions mentioned earlier are not executed, Instead, it handles the push in the app's applicationdidfinishlaunching function, which gets the data from the push message in the following code: Nsdictionary *userinfo =[ launchoptionsobjectforkey:uiapplicationlaunchoptionsremotenotificationkey];            Two. When the app is in the foreground, the system receives the push message, the system will not pop up a message prompt, will directly trigger application: (UIAPPLIcation *) Application didreceiveremotenotification: (nsdictionary *) userinfo function, push data in UserInfo dictionary.              When the app is in the background, if the system receives a push message, the application will be executed when the user clicks the push message: (UIApplication *) Application didreceiveremotenotification: (nsdictionary *) userinfo functions,          The order of functions in Appdelegate is now:          applicationwillenterforeground           application:didreceiveremotenotification          applicationdidbecomeactive

iOS Push Summary (certificate generation, client development, server development)

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.