IOS push Summary (certificate generation, client development, server development)

Source: Internet
Author: User
Tags key string pkcs12 ssl connection

1. Introduction to the push Process (1) when the App is started, use the UIApplication: registerForRemoteNotificationTypes function to communicate with Apple's APNS server and send a registration remote push application. If the registration is successful, the callback function application :( UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) The deviceToken is triggered, and the App can get the deviceToken, which is a device-related string. (2) After the App obtains the DeviceToken, it sends the DeviceToken to its server. (3) After obtaining the DeviceToken, the server uses the Certificate file to initiate an SSL connection to the APNS server of apple. After the connection is successful, send a JSON string that contains the type and content of the push message. (4) After Apple's APNS server receives a JSON string, it sends a notification message to the App so that the App's callback function application :( UIApplication *) application didreceiveremotenotif:( NSDictionary *) userInfo is called, the App can get the content of the push message from userInfo. 2. Used Certificate file and Generation Process (1) certSigningRequest file, which is generated on the MAC system and used to apply for pushing certificate files on the Apple website. Generation Process: Open the "key string access" software in the application, select "key string access"-"Certificate assistant"-"request certificate from Certificate Authority" from the menu ", enter the email address and name, and save the email address to the disk to generate a CertificateSigningRequest. the certSigningRequest file. (2) register an app id that supports push, which will be used later. Generation Process: Go to pai.apple.com, select member center-Certificates, Identifiers & Profiles-Identifiers-App Ids, then select register app id, set the appid name, in the app id suffix column, you must select the explicit app id, set the bundle id, and then select Push Notifications in App Services. In this way, you can register an aphid that supports push. (3) Push the certificate cer file, which is generated in pai.apple.com and used to generate the files required by the server. Generation Process: Go to pai.apple.com, select member center-Certificates, Identifiers & Profiles-Certificates, and create certificate. The types are Development and Product. Take Development as an example. Select Apple Push Notification service SSL (Sandbox), next, select the previously generated push-supported AppId, and then click Next to submit the previously created CSR file, next, you can generate the cer file and save it to your local device. (4) generate the Certificate file used by the server. If you use the Internet mac version PushMeBaby tool to send push messages on mac machines, the above cer file is enough. If you are using PHP, java/c # to develop your own server, you also need to convert the above cer file to generate a pem file or p12 file. The process of generating a pem file for php is as follows: first, double-click the previously saved cer file. The "key string access" software will be opened, and an Apple Development IOS push services certificate will appear in it, A public key and a private key. The name of the key is the same as the name entered in the certificate assistant. Select a certificate, export as apns-dev-cert.p12 files select a private key, export as apns-dev-key.p12 files convert these files to PEM format through 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-dev-key.p12 finally, you need to combine two pem files into a apns-dev.pem file, this file requires the use of: cat apns-dev-cert.pem apns-dev-key-noenc.pem> apns-dev.pem to generate a p12 file for java/c # When connecting to APNS process: openssl pkcs12-clcerts-nokeys-out apns-d Ev-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) generates the provisioning file used by XCODE, which is used for real-machine debugging. Generation Process: Go to pai.apple.com, select member center-Certificates, Identifiers & Profiles-Provisioning Profiles, create Provisioning file, select iOS App Development, and then select AppId, select the previously created push-supported appid, then select the certificate that supports push in the next step, select the device id to be supported in the next step, and set the file name of the provisioning file in the last step, so that the provisioning file is generated. 3. server development (1) if you only want to test message pushing 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 the https://github.com/stefanhafeneger/PushMeBaby, the Code Execution Process is actually set up the SSL Certificate, then connect to APNS, then send JSON data. Because the SSL logic is to be processed, the Code is a little bit more. When using the tool, replace the cer file in the project resource with your own cer file, and then replace the deviceToken in the Code with the deviceToken of your device. (2) Use php to develop the server. Because php has a built-in ssl module, the process of connecting to the APNS server to send json is actually very simple. The Code is as follows: this file can be stored on the server and accessed through a browser. It can also be interpreted and executed using the command line. The code is $ php-f Pusher. php copy Code <? Php $ deviceToken = 'your devicetoken'; // No space $ body = array ("aps" => array ("alert" => 'message ', "badge" => 2, "sound" => 'default'); // push method, including content and sound $ ctx = stream_context_create (); // if there is a problem with finding the pem path on a Windows Server, change the path to the following method: // $ pem = dirname (_ FILE __). '/'. 'apns-dev. pem'; // the linux server can directly write the pem path to stream_context_set_option ($ ctx, "ssl", "local_cert", "apns-dev.pem"); $ pass = "xxxxxx "; stream_context_set_option ($ ctx, 'Ssl ', 'passphrase', $ pass); // you need to select two servers, select the server with the second sandbox name and use the Dev pem certificate. If it is officially released, use the Product pem and select the official server $ fp = stream_socket_client ("ssl: // gateway.push.apple.com: 2195 ", $ err, $ errstr, 60, STREAM_CLIENT_CONNECT, $ ctx); $ fp = stream_socket_client (" ssl: // gateway.sandbox.push.apple.com: 2195 ", $ err, $ errstr, 60, 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 (1) download the previously created cer and provisioning files, double-click them, import them to xcode, and select the names of the two files in the code signing column of build setting, in this way, you can deploy apps that support push to a real machine. (2) the processing of the push message client can be divided into two situations: 1. When the App is not running, The system receives the PUSH message, the user clicks push message, and starts the App. In this case, the didReceiveRemoteNotification function is not executed, but the push is processed in the applicationDidFinishLaunching function of the App. The following code can be used to obtain data in the push message: NSDictionary * userInfo = [launchoptionsobjec II. when the APP is on the frontend, The system receives the PUSH message. In this case, no message is displayed, and application :( UIApplication *) application didReceiveRemoteNotification (NSDictionary *) userInfo function is directly triggered, push data in the userInfo dictionary. If the App is in the background and the system receives the PUSH message, when the user clicks push message, the application (UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo function is executed, in this case, the function execution sequence in AppDelegate is applicationWillEnterForeground application: didReceiveRemoteNotification applicationDidBecomeActive.

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.