Push Implementation of the iPhone message Push mechanism and send messages to ios applications through. net Applications

Source: Internet
Author: User
Tags openssl x509 pkcs12 ssl certificate
Push principle:
The working mechanism of Push can be summarized

In the figure, Provider refers to the Push server of an iPhone software. In this article, I 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: the. net 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 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, a PushChat. certSigningRequest file is generated in the response.
Ii. Generate the *. p12 File
1. Export the key and enter your password.

Enter your password:

In this way, a PushChatKey. p12 file is generated.
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:
(Uiremotenotiftypetypebadge | uiremotenotiftypetypesound | UIRemoteNotificationTypeAlert)];

Return YES;
}

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 to generate a certificate file for. net and APNS communications.
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. Here the password is set 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_polic_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 /.
We can download the source code and make corresponding adjustments to the JdSoft. Apple. Apns. Notifications in it.
We adjusted the JdSoft. Apple. Apns. configurations. Test code 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 sendNotification 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 sentif (service. QueueNotification (alertNotification ))
Console. WriteLine ("Notification Queued! ");
ElseConsole. WriteLine ("Notification Failed to be Queued! ");

// Sleep in between each messageif (I <count)
{
Console. WriteLine ("Sleeping" + sleepbetweenications ications + "milliseconds before next Notification ...");
System. Threading. Thread. Sleep (sleepbetweenications ications );
}
}

Conclusion: first of all, you need to understand the principles, principles, and things. Specific operations are not difficult. I hope they will help you!

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.