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) During the app launch, use the Uiapplication::registerforremotenotificationtypes function to communicate with Apple's APNs server and issue a registered 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.
(2) After the app gets to Devicetoken, it sends the Devicetoken to its own server.
(3) After the service side gets Devicetoken, use the certificate file to initiate an SSL connection to Apple's APNs server. After the connection is successful, send a JSON string containing the type and content of the push message.
(4) After the Apple APNs server gets the JSON string, it sends a notification message to the app, making 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 process

(1) The Certsigningrequest file, which is generated on the Mac system, 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) Register an app ID that supports push, which will be used later.
Build process:
Enter developer.apple.com, select Member Center-certificates, Identifiers & Profiles-identifiers-app IDs, then select Register App ID, set a Ppid name, meanwhile, 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 so that you can register a aphid that supports push.


(3) The push certificate CER file, which is generated in developer.apple.com, is used 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.

(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.

        the process for generating a PEM file for PHP is :
        First double-clicking the previously saved CER file opens the Keychain Access software, an Apple development  ios push Services certificate appears, a public key and a private key with the name of the key that matches the name that is filled in in the certificate assistant.
      Check certificate, export as  APNS-DEV-CERT.P12 file
      Select private key, export as Apns-dev-key.p12 file
& nbsp      convert these files to PEM format via terminal commands:
      OPENSSL pkcs12-clcerts-nokeys-out Apns-dev-cert.pem-in APNS-DEV-CERT.P12
      OpenSSL pkcs12-nocerts-out apns-dev-key.pem-in apns-de V-KEY.P12
      Finally, you need to combine two PEM files into a single APNS-DEV.PEM file that you need to use when connecting to APNs:
      Cat Apns-dev-cert.pem Apns-dev-key-noenc.pem > Apns-dev.pem

        Generate java/c# The procedure for using the P12 file is :

OpenSSL pkcs12-clcerts-nokeys-out apns-dev-cert.pem-in APNS-DEV-CERT.P12
OpenSSL pkcs12-nocerts-out apns-dev-key.pem-in APNS-DEV-KEY.P12
OpenSSL pkcs12-export-in Apns-dev-cert.pem-inkey apns-dev-key.pem-certfile Certificatesigningrequest.certsigningrequest-name "Push"-out PUSH.P12


(5) Generate the provisioning file used by Xcode 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
(1) If you just want to test the push of a message on a Mac computer, you can use the Pushmebaby tool, which 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.


(2) Using PHP Development Server
Since PHP already has a built-in SSL module, the process of using PHP to connect to the APNS server to send JSON is actually very simple, and the code is as follows:

The file can be accessed through a browser on the server, or it can be interpreted by the command line, with the following code: $ php-f pusher.php

 <?php$devicetoken= ' own devicetoken ';//no spaces 
$body = Array ("APS" = = Array ("Alert" = "message", "badge" = 2, "sound" = ' default '); Push mode, containing 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 writes the path of the PEM directly
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 you are developing a test, select a second sandbox server and use Dev's PEM certificate, if it is officially released, use product's PEM and choose a formal server
$fp = Stream_socket_client ("ssl://gateway.push.apple.com:2195", $err, $errstr, A, stream_client_connect, $ctx);
$fp = Stream_socket_client ("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, $, stream_client_connect, $ CTX);

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

4. Client-side development
(1) Download the previously established CER file and provisioning file, double-click, import into Xcode, in the build setting Code signing column, select the name of these two files, so that the push-enabled app can be deployed to the real machine.

(2) Handling push messages
Client handling of push messages is divided into two scenarios:
When the app is not running, the system receives a push message and the user clicks the push message to launch the app. Instead of executing the previously mentioned didreceiveremotenotification function, the push is processed in the app's applicationdidfinishlaunching function, and the data in the push message can be obtained from the following code: NSDi Ctionary *userinfo =[launchoptionsobjectforkey:uiapplicationlaunchoptionsremotenotificationkey];


Two. When the app is in the foreground, the system receives the push message, the system will not pop up the message prompt, will directly trigger application: (UIApplication *) application didreceiveremotenotification: ( Nsdictionary *) UserInfo function, pushes the data in the UserInfo dictionary.

When the app is in the background, if the system receives a push message, when the user clicks the push message, the application is executed: (uiapplication *) application didreceiveremotenotification: ( Nsdictionary *) UserInfo function,
The order in which the functions are executed at this time is: appdelegate
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.