IOS ----- push mechanism (lower), ios ----- push mechanism

Source: Internet
Author: User

IOS ----- push mechanism (lower), ios ----- push mechanism
Push-to-machine (lower)

 

Save the Certificate Signature request file to the disk. Save the file as "Push. CerSigningRequest ".

 

On the displayed page, you can see that the "iOS Apps" section contains Certificates, Identifiers, Device, ProvisioningProfiles ----- only the reasons why the Logon account has already joined the iOS Developer Program (iDP.

 

Lists all the App IDs currently owned by the developer account.

 

The description string of the App ID can be entered as needed, but the unique ID of the App ID must be remembered. Generally, the format of "Company domain name + application name" is used.

 

 

 

 

 

 

You can also Download the specified certificate on the certificate list page. (If you click the specified certificate, the page displays the "Revoke" and "Download" buttons. The "Revoke" button is used to delete the certificate, the "Download" button is used to Download the certificate ).

After the above steps, we have successfully created an App ID that supports Push notification for the Push client, and downloaded and installed the development certificate for this App ID.

Now, start to develop the Push 'client application. Create a Single View Application. The Bundle Identifier of the Application must be exactly the same as the previously registered App ID, as shown in the create Application dialog box.

This dialog box is critical. You must ensure that the "Bundle Identifier" used for this dialog box is exactly the same as the previously registered App ID.

 

Next, modify the application delegate class to register the remote Push notification, and rewrite the corresponding method to process the remote Push notification.

 

 

It is not difficult to see from the above introduction that it is relatively simple for iOS apps to process remote notifications.

Partial code of the application delegate class in this example

AppDelegate. m-(BOOL) application: (UIApplication *) application alert: (NSDictionary *) LaunchingOptions {// register the remote push notification [[UIApplication notification application] Alert: UIRemoteNotificationTypeBadge | uiremotenotiftypetypesound | alert]; return YES; // ①}-(void) application: (UIApplication *) application didRegisterForRemoteNotifica TionsWithDeviceToken: (NSData *) pToken {NSLog (@ "registered successfully: % @", pToken); // registered successfully, the device token should be sent to the Push server program // The Push server program should save the token to the database for future reuse}-(void) application: (UIApplication *) application didFailToRegisterForRemoteNotifications WithError: (NSError *) error {NSLog (@ "registration failed: % @", error);}-(void) application: (UIApplication *) application didReceiveRemoteNotification: (NSDictionary *) userInfo {// Process PUSH message UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "message" message: @ "" delegate: self cancelButtonTitle: @ "cancel" otherButtontitles: nil]; [alert show]; NSLog (@ "% @", userInfo );}... @ End

 

The Code in line 2 of the above program registers the remote Push notification. Next, the program implements three methods related to the remote Push notification in the UIApplicationDelegate protocol-methods triggered when the Push notification is successfully registered or fails, and the method triggered when a remote Push notification is received.

Applications that remotely push notifications must be tested on a real machine, and client applications also need Provisioning Profile, which must be created and downloaded from the Apple website. To create or download a Provisioning Profile for a remote push notification application, follow these steps.

 

Among them, the first Provisioning Profile is the Provisioning Profile for testing other common programs. Because the so-called App ID of other programs supports wildcards, only one Provisioning Profile is required. However, the App ID of the remote push notification application cannot use wildcards, so a new Provisioning Profile must be created here.

 

 

 

 

 

 

 

Compile and run the application (run the application on a real machine). The prompt box shown in is displayed.

 

You can click the "OK" button to view the output shown in at the bottom of Xcode.

 

The iOS app successfully registers the remote push notification, and APNs returns the device token of the device. in practical applications, the iOS app should send the device token to the Push server program through the network. Here we do not add the proxy code for network transmission to simplify programming, instead, the device token is directly copied to the Push server program.

 

Develop Push server programs

The Push server program must contain the SSL connection certificate and private key. Therefore, you must prepare the Push server programs before developing them.

Import the private key of the remote push notification application

 

The reason for listing the public key and private key named "Push" is that the Certificate assistant created the Certificate Signing Request file named Push when the iOS client application was developed, if the Certificate Signing Request file is not created or the name specified when the Certificate Signing Request is created is different, the public key and private key named "Push" are not displayed here.

 

 

Now we have obtained the private key and SSL connection certificate required by the Push server program (the development certificate has been downloaded from the Apple website before the development of the Push notification client, the name of the Development Certificate saved at that time is aps_development.ver ).

Next, you need to merge the two files into one. follow these steps:

  • Opensel x509-in aps_development.cer-inform der-out PushCert. pem-outform PEM // ①

The preceding command is used to convert the aps_development.cer Certificate file to a PEM Certificate file. This command will generate a PushCert. pem file.

  • Opensel pkcs12-nocerts-in Push. p12-out PushKey. pem // ②

The preceding command is used to convert the Push. p12 private key file to a PEM private key file. This command will generate a PushKey. pem file.

If you run the preceding command, you will be prompted to enter the password three times: 1st times to enter Push. the p12 Private Key File Password (that is, the password we entered in the "Enter password for Push" image). Only by entering this password can the openssl command read the Push. p12 private key file content; the password entered 2nd times will be used as PushKey. the password of the pem file. The password entered 3rd Times is used to confirm the password entered 2nd times ----- "user" is still entered here ".

  • Openssl pkcs12-export-in PushCert. pem-inkey PushKey. pem-out aps_developer_identity.p12 // ③

The preceding command combines PushCert. pem and PushKey. pem into an aps_developer_identity.p12.

If you run the preceding command, you will be prompted to enter the password three times: 1st times and PushKey. the password of the pem file (that is, the PushKey in step 1. the password entered in the pem file). Only the password can be entered to allow the openssl command to read the PushKey. content of the pem file; the password entered 2nd times will be used as the password of the aps_0000_identity.p12 file; the password entered 3rd Times is used to confirm the password entered 2nd times ----- enter "user" here ".

Shows the entire running process.

 

Java has a JavaPNS open-source project for developing the Apple Push Notification Service Provider. With this project, you can easily develop a remote Push Notification server program. To download and install JavaPNS, follow these steps:

JavaPNS_2.2.jar: This option only downloads the core JAR package of the JavaPNS project.

JavaPNS_2.2_javadoc.zip: This option only downloads the API documentation of the JavaPNS project.

JavaPNS_2.2_complete.zip: This option downloads the full package of JavaPNS, including the core JAR package of JavaPNS, API documentation, and examples.

Doc: this folder contains various documents of the JavaPNS project.

Lib: The file contains two JAR packages: bcprov-jdk15-146.jar and log4j-1.2.15.jar. The two JAR packages are

JAR package on which JavaPNS depends.

Src: this folder contains the source code of the JavaPNS project.

JavaPNS_2.2.jar: this is the core JAR package of the JavaPNS project.

Write a simple Java source file as follows to implement PushSever of the server program for remote notification push. javaimport javapns. push; import javapns. notification. pushNotificationPayload; public class PushServer {public static void main (String [] args) {// The device token that sends the Push notification to the device. // The device token of different devices should be sent by the iOS application to the server program through the network. // The server program should save the device token in in the server // then send the Push notification String deviceToken = "60c70bb185cd50edfaa430" to each device token cyclically "; // ① try {// create PushNotificationPayload pushnotifpaypayload payload = new PushNotificationPayload ();

// Set the PUSH message body payload. addCustomAlertBody ("push notification \ n happy new year. \ N "+" 2016, smooth, secure "); // set the value payload in the Red Circle of the application icon. addBadg (2); // set the sound of the push notification to payload. addSound ("default"); // send Push notification Push. payload (payload, ".. /aps_developer_identity.p12 ", // specify the file" user "that contains the certificate and private key, // set the export password of the aps_developer_identity.p12 file to false, // whether it is the deviceToken in the productization stage ); // ②} catch (Exception e) {e. printStackTrace ();}}}

 

In the above Code, the 1st lines of red text indicates the device token of the iOS device. In actual applications, the device token should be sent to the server by the iOS client over the network. For simplicity, copy and paste the device token of the iOS device here.

The code in section 2nd of the above program uses the payload () method of the Push class to send remote Push notifications, the push notification is sent by specifying the export password of the file containing the certificate and private key and the aps_mirror_identity.p12 file.

Copy the JavaPNS_2.2.jar, bcprov-jdk15-146.jar, and log4j-1.2.15.jar in the JavaPNS decompression path to the same path of the Java source file, and then compile the Java source file using the following command:

Java-encoding UTF-8-cp JavaPNS_2.2.jar PushServer. java // ①

The above command is used to compile the PushServer. java file. This command uses the-cp option to temporarily add JavaPNS_2.2.jar to the class loading path.

Run PushServer with the following command:

Java-cp.: JavaPNS_2.2.jar: bcprov-jdk15-146.jar: log4j-1.2.15.jar PushServer // ②

The above command is used to run PushServer and uses the-cp option to temporarily add JavaPNS_2.2.jar, bcprov-jdk15-146.jar, and log4j-1.2.15.jar to the class load path

/*

Note:

If the preceding java commands are executed on UNIX, Linux, and OS X systems, the separators of the JAR packages in the-cp option values are colons; if the preceding java command is executed in Windows, replace the separators of the JAR packages in the-cp option value with semicolons. In addition, do not forget the point (.) at which the-cp option value starts. This point (.) indicates the current path and is used to tell the system to search PushServer files in the current path.

*/

 

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.