[IOS] Implementation of iOS message push mechanism and ios message push mechanism

Source: Internet
Author: User
Tags openssl x509 pkcs12 ssl certificate

[IOS] Implementation of iOS message push mechanism and ios message push mechanism

The working mechanism of iOS message push can be summarized as follows:

 

Provider refers to the Push server of an iPhone software. APNS is the abbreviation of Apple Push Notification Service and is the server of Apple.

 

There are three phases:

Phase 1: The application packs the message to be sent and the identifier of the target iPhone and sends it to APNS.

Stage 2: APNS searches for the iPhone with the corresponding identifier in its registered Push Service iPhone list and sends the message to the iPhone.

Stage 3: The iPhone transmits the sent message to the corresponding application, and a Push notification is displayed according to the settings.

 

We can see that:

1. The application registers for message push.

2. iOS obtains the device token from the APNS Server, and the application receives the device token.

3. The application sends the device token to the PUSH server program.

4. The server program sends a message to the APNS service.

5. The APNS Service sends messages to the iPhone application.

 

Both the iPhone client and APNS, Provider and APNS, must be connected through a certificate.

 

Below I will introduce several certificates used.

 

1. CSR File

 

1. Generate a Certificate Signing Request (CSR)

 

2. Enter your email address and common name, and save it to the hard disk.

 

Click to continue:

 

In this way, a Push. certSigningRequest file is generated in the response.

 

Ii. p12 files

 

1. Export the key.

 

2. enter your password.

 

 

In this way, a Push. p12 file is generated.

 

Iii. SSL certificate file

 

1. log on to the iOS Provisioning Portal with the account you paid, and create an App ID. For details about this process, refer to debugging the iOS App's real machine, this will generate the following record:

 

2. Click Configure on the right:

 

3. Click the Configure next to the SSL Certificate row pushed by Development:

 

 

4. Click "Continue:

 

5. Select the previously generated Push. certSigningRequest file and click Generate. The following page is displayed:

 

6. Click "Continue:

 

7. Click Download and name the file aps_developer_identity.cer.

 

8. Click Done and you will find that the status has changed to Enabled:

 

Note: The Apple Push Notification service column of some App IDS is gray and the Configure button is not allowed because APNS does not support App IDs with wildcards.

 

So far, we have generated three files:

 

1. Push. certSigningRequest

2. Push. p12

3. aps_developer_identity.cer

 

Add the following code to the didfinishlaunchingwitexceptions method of the AppDelegate project:

 

Ios code
  1. [[UIApplication sharedApplication] registerForRemoteNotificationTypes :( UIRemoteNotificationTypeAlert | uiremotenotiftypetypesound | uiremotenotiftypetypebadge)];

 

The registerForRemoteNotificationTypes method is used to notify the application to receive push notifications.

 

In the project's AppDelegate, add the following method to obtain the deviceToken:

 

Ios code
  1. -(Void) application :( UIApplication *) app didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken {
  2. NSString * token = [NSString stringWithFormat: @ "% @", deviceToken];
  3. NSLog (@ "My token is: % @", token );
  4. }
  5. -(Void) application :( UIApplication *) app didFailToRegisterForRemoteNotificationsWithError :( NSError *) error {
  6. NSString * error_str = [NSString stringWithFormat: @ "% @", error];
  7. NSLog (@ "Failed to get token, error: % @", error_str );
  8. }

 

We can submit the obtained deviceToken to the background application. The background application that sends the notification must know the deviceToken and a certificate connected to APNS.

 

This certificate can be obtained from the two files we generated earlier.

 

1. Convert aps_developer_identity.cer to aps_developer_identity.pem format.

 

Shell code
  1. Openssl x509-in aps_developer_identity.cer-inform DER-out aps_developer_identity.pem-outform PEM

 

2. Convert the private key in p12 format to pem

 

Shell code
  1. Openssl pkcs12-nocerts-out Push_Noenc.pem-in Push. p12

 

3. Create a p12 File

 

Shell code
  1. Openssl pkcs12-export-in aps_developer_identity.pem-inkey Push_Noenc.pem-certfile Push. certSigningRequest-name "aps_developer_identity"-out aps_developer_identity.p12

 

In this way, we get the certificate file used in background applications such as. net or java: aps_rj_identity.p12

 

If the background application is php, you can generate the Certificate file used in the php background application according to the method in this article generated by the pem file in the iOS message push mechanism: ck. pem

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.