IOS message push mechanism Principle and Implementation)

Source: Internet
Author: User
Tags openssl x509 pkcs12

Today, we have developed an apple client push interface for our colleagues in the wireless business department. The wireless business department is our department dedicated to developing apps for various terminals!

Colleagues in the wireless business department are considerate. They also sent me the reference document address and required pl2 files for development.

The documentation provides detailed descriptions of the push principles and related application steps and some cocoa and. netCodeAnd provides open-source. Net class libraries. I am very grateful to my colleagues in the wireless business department, O (Alipay _ Workshop) O haha.

Documentation address: http://hi.baidu.com/yang_qi168/item/480304c542fd246489ad9e91

The original text is as follows:

Push principle:

The working mechanism of push can be summarized

In the figure, provider refers to the push server of an iPhone software.ArticleI will use. NET as the provider.

Apns is the abbreviation of Apple push notification service (Apple push server). It is an apple server.

It can be divided into three stages.

Phase 1:. Net ApplicationsProgramPackage the message to be sent and the ID of the target iPhone 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 from.

 

1. First, the application registers for message push.

2. for iOS and apns servers, devicetoken is required. The application accepts devicetoken.

3. The application sends the devicetoken to the push server.

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.

Several certificates:

1. *. certsigningrequest File

1. Generate a Certificate Signing Request (CSR ):

 

2. Enter your mailbox and common name. Enter pushchat here. Select Save to hard disk.

 

In this way,Pushchat. certsigningrequestFile.

Ii. Generate the *. p12 File

1. Export the key and enter your password.

 

Enter your password:

 

In this way,Pushchatkey. p12File.

3. Create an app ID and SSL Certificate file

1. log on to the IOS provisioning portal with your paid Apple account. Create an app ID.

Description: Enter pushchat

Bundle seed ID: generate new by default

Bundle identifier: Enter com. mysoft. pushchat

Click to submit

 

This will generate the following record:

 

Click Configuration:

The following page appears. Click continue:

 

Here we select the previously generated pushchat. certsigningrequest file and click Generate.

 

Generating

 

After the generation is completed, download it. The name is aps_developer_identity.cer.

 

Click Finish and you will find the status changed to enabled.

 

So far, we have generated three files.

1. pushchat. certsigningrequest

2. pushchatkey. p12

3. aps_developer_identity.cer

Now we create a simple iPhone application.

1. Open xcode and choose to create a view-based application. The name is as follows:

 

2. Add the following code to the didfinishlaunchingwitexceptions method in pushchatappdelegate:

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
{
Self. Window. rootviewcontroller = self. viewcontroller;
[Self. Window makekeyandvisible];

// Let the device know we want to receive push notifications[[Uiapplication sharedapplication] registerforremotenotificationtypes:
Uiremotenotificationtypebadge | uiremotenotiftypetypesound | uiremotenotificationtypealert)];
ReturnYes;
}

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

3. When running in xcode, the following prompt box is displayed:

 

Select OK. Indicates that the application has enabled the Message notification service.

In the pushchatappdelegate. m code, add the following method to obtain the devicetoken:

-( Void ) Application :( uiapplication *) Application didregisterforremotenotificationswithdevicetoken :( nsdata *) devicetoken
{
Nslog ( @ "My token is: % @" , Devicetoken );
}

-(Void) Application :( uiapplication *) Application didfailtoregisterforremotenotificationswitherror :( nserror *) Error
{
Nslog (@ "Failed to get token, error: % @", Error );
}

After obtaining the devicetoken, we can submit it to the. NET application through the WebService. Here I simply process it, print it directly, and copy it to the. NET application environment for use.

The. NET application that sends the notification needs to know the devicetoken and a certificate to connect to apns.

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

Use OpenSSL

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

OpenSSL X509-In aps_developer_identity.cer-inform der-out aps_developer_identity.pem-outform PEM

2. to convert a private key in p12 format to PEM, you need to set the password four times and set the password to ABC123.

OpenSSL PKCS12-nocerts-out pushchat_noenc.pem-In pushchat. p12

3. Use Certificate and the key to create a file in PKCS #12 format.

OpenSSL PKCS12-export-In aps_developer_identity.pem-inkey pushchat_noenc.pem-certfile pushchat. certsigningrequest-name "aps_developer_identity"-out aps_developer_identity.p12

In this way, we get the certificate file used in the. NET application:Aps_developer_identity.p12.

Send notifications in. NET applications.

There is an open-source Class Library: apns-sharp.

The address is http://code.google.com/p/apns-sharp /.

DownloadSource codeYou can adjust the jdsoft. Apple. apns. configurations.

We adjusted jdsoft. Apple. apns. configurations. Test Based on devicetoken and p12file, as shown in.

 

In this case, OK.

Effect:

Notification code:

For ( Int I = 1 ; I <= count; I ++)
{
// Create a new notification to send
Notification alertnotification = New Notification (testdevicetoken );

Alertnotification. payload. Alert. Body =String. Format ("Testing {0 }...", I );
Alertnotification. payload. Sound ="Default";
Alertnotification. payload. Badge = I;

// Queue the notification to be sent
If(Service. queuenotification (alertnotification ))
Console. writeline ("Notification queued! ");
Else
Console. writeline ("Notification failed to be queued! ");

// Sleep in between each message
If(I <count)
{
Console. writeline ("Sleeping"+ Sleepbetweennotifications +"Milliseconds before next notification ...");
System. Threading. thread. Sleep (sleepbetweenications ications );
}
}

The following figure shows an iPad shot on a mobile phone:

 

 

conclusion: This article describes in detail how to implement the IOS message push mechanism and how to send messages to iOS applications through the. NET application.

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.