The implementation and discussion of the iphone message push mechanism

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

The last two days are studying the messaging push mechanism for iOS. The study of this thing is still full of interest.

The principle of push:

The working mechanism of Push can be summarized simply as

In the figure, provider is the push server for an iphone software, which I will use. NET as the provider.
APNS is an abbreviation for Apple push Notification Service (Apple push server) and is Apple's server.

Can be divided into three stages.

Phase one:. NET applications package the message to be sent, the identity of the target iphone, and send it to APNs.
Phase II: APNs in its own list of iphone's registered push services, look for an iphone with a corresponding logo and send the message to the iphone.
The third stage: The iphone sends the message to the appropriate application and follows the settings to eject the push notification.

From what we can see.

1, the first is the application registration message push.

2, iOS and APNs server to Devicetoken. The application accepts Devicetoken.

3. The application sends the Devicetoken to the push service-side program.

4. The service-side program sends messages to the APNS service.

5. The APNs service sends the message to the iphone app.

Both the iphone client and APNs, provider and APNs need to be connected through a certificate. Let me introduce some of the certificates used.

Several certificates:

I.. *.certsigningrequest file

1. Generate Certificate Signing Request (CSR):

2, fill in your mailbox and Common Name, here fill in for Pushchat. Select Save to hard disk.

This generates a Pushchat.certsigningrequest file locally.

Second, generate *.P12 file

1. Export the key and enter your password.

Enter your password:

This generates a PUSHCHATKEY.P12 file.

III. Create a new app ID and SSL certificate file

1. Sign in to iOS Provisioning Portal with your paid Apple account. Create a new app ID.

Description: Medium Input Pushchat

Bundle Seed ID: Default selection Generate New

Bundle Identifier: Input com.mysoft.PushChat

Click Submit

This results in the following record:

Click Configure:

The following screen appears, click Continue:

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

is generating

After the build, we download it. Name is Aps_developer_identity.cer.

Click Done and you will see that the status becomes enabled.

So far, we have generated 3 files.

1, Pushchat.certsigningrequest

2, PUSHCHATKEY.P12

3, Aps_developer_identity.cer

Now we create a simple iphone app.

1. Open Xcode and choose to create a view-based application. Names such as:

2. Add the following code to the Didfinishlaunchingwithoptions method in Pushchatappdelegate:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Self.window.rootViewController = Self.viewcontroller;
[Self.window makekeyandvisible];

Let the device know we want to receive push notifications
[[UIApplication sharedapplication] Registerforremotenotificationtypes:
(Uiremotenotificationtypebadge | Uiremotenotificationtypesound | Uiremotenotificationtypealert)];

return YES;
}

The Registerforremotenotificationtypes method tells the application that it can accept push notifications.

3, run in Xcode, will pop up the following prompt box:

Select OK. Indicates that this application turns on the message notification service.

Add the following method in the PUSHCHATAPPDELEGATE.M code to get 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);
}Get to the devicetoken that we can submit to. NET applications through the WebService service, where I simply process it, print it directly, and copy it to a. NET application environment.

A. NET application that sends a notification needs to know Devicetoken, and requires a certificate that is connected to the APNs.

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

Use OpenSSL to generate the certificate files 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, the P12 format of the private key conversion to PEM, you need to set 4 times password, where the password is set to: abc123.

OpenSSL pkcs12-nocerts-out pushchat_noenc.pem-in PUSHCHAT.P12

3. Create pkcs#12 format files with certificate and the key.

OpenSSL pkcs12-export-in aps_developer_identity.pem-inkey pushchat_noenc.pem-certfile PushChat.certSigningRequest- Name "Aps_developer_identity"-out aps_developer_identity.p12

This way we get the certificate file used in the. NET application: APS_DEVELOPER_IDENTITY.P12.

Send notifications in a. NET application.

There is an open-source class library: Apns-sharp.

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

We download the source code, the inside of the JdSoft.Apple.Apns.Notifications to make corresponding adjustments can be used.

We adjust the JdSoft.Apple.Apns.Notifications.Test code according to Devicetoken and P12file, for example.

This will be OK.

Effect:

Code for the notification:

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 (sleepbetweennotifications);
}
}

The display on the ipad that was taken on the phone:

Summary: This article is mainly about the implementation of the iOS message push mechanism, how to send it through a. NET application message to the iOS app.

The implementation and discussion of the iphone message push mechanism

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.