Implementation of message push and principle analysis in IOS _ios

Source: Internet
Author: User
Tags openssl openssl x509 php code

First, the message pushes the principle:

Mention several push-related concepts before implementing a message push, as shown in Figure 1-1:

1, Provider: is to specify the iOS device application to provide a push server, (if the iOS device application is a client, then Provider can be understood as the service side [originator of the message]);

2, Apns:apple push Notification service[Apple message Push server];

3, the IPhone: To receive APNs sent down the message;

4, client App:ios device application, used to receive the iphone delivery APNs sent message to the development of a client app[message of the final responder;

The above diagram can be divided into three stages:

Stage One: provider[Server] To send the message, the purpose of the iOS device identification packaging, sent to APNs;

Stage Two: APNs, in its own list of registered push services iOS devices, looks for iOS devices that have a corresponding identity and sends messages to iOS devices;

Phase III: The iOS device sends the message to the corresponding application and pops the push notification as set.

The specific process, figure 1-2 below:

1, [Client APP] registered message push;

2, [client app] and [APNs Service] to Devicetoken, Client app to receive Devicetoken;

3, [Client APP] will Devicetoken sent to [Provider]push server program;

4, when the push server program to meet the conditions for sending messages, [Provider] to [APNs service] to send messages;

5. [APNs Service] sends the message to [Client APP].

Second, message push implementation:

1, generate *.certsigningrequest files, the following steps:

[macbook-Application-utility-Keychain Access-Certificate assistant-ask for certificate from Certificate Authority-Certificate information (user e-mail address {Fill in your mailbox, such as:}, common name {any, such as: Pushdemo}, request is: {single, select ' Store to disk '} '-continue-save], at which point the file you specify will be generated in the location you specify. The default is Certificatesigningrequest.certsigningrequest file, which is named: Pushdemo.certsigningrequest. This *.certsigningrequest has been generated , the detailed steps are shown in the figure.

If the build succeeds, the key associated with *.certsigningrequest is listed in the Keychain access | login | Key column, which is Pushdemo, as shown in the figure:

2. Create a new app ID (configured in the Apple developer account)

(1) Login to iOS Dev Center, after successful login, click (iOS Provisioning Portal link), as shown in the figure:

(2) Create new app Id[app idsàmanageànew app id] (description{fill in your description of this app ID, such as: Ishop},bundle Seed ID (App ID Prefix) {Select Bind app ID Prefixes, such as: Default selection Generate New},bundle Identifier (app ID Suffix) {Fill in the binding App ID suffix, such as: Com.yourcorp.iShop}), as shown in the following illustration:

This will generate the following record, as shown in the figure:

(3) Configure the app ID generated in the previous step to support message push [click the Configureà in 2-6 to select the Enable for Apple push Notification Serviceà Click Configure] as shown in the figure:

(4) Generate a certificate signing request (Generate deployment certification) [Click on 2configureàcontinueà Step 1 in 2-7 to generate the * Certsigningrequest file (this is ishop.) Certsigningrequest)-generate-is downloaded after the build is completed, named: Aps_developer.cer], double-click Aps_developer.cer Certificate {Associate the certificate with the key, and import the certificate into the MacBook , as shown in the following illustration:

(5) Create development Provisioning profiles[Development License profile] (provisioning| Development| New profile), the details of the operation process as shown below:

Click Submit in the map to generate development provisioning profiles[Development License profile], here is: Ishopdevprofile.mobileprovision as shown in the following figure:

Download this development license (for online debugging).

Summary, so far we have generated: a:*.certsigningrequest files (used in step (4) to generate certificate B), B:aps_developer_identity.cer certificate (in Provider[push server) Service-side application), c:* ... Mobileprovision Develop the license profile (used for online debugging on client app clients).

3. Create a new project

1. Create a "single View Application" Project for easy, you set the "company Identifier" + "Production" must and step 5 created by the app ID "bundle Identifier "consistent.
2. In APPDELEGATE.M file's "Didfinishlaunchingwithoptions" method, add the following code (for the app Register push notification feature):
//Let the device know we want to receive push notifications
[[UIApplication sharedapplication] Registerforremotenotificationtypes:
(Uiremotenotificationtypebadge | Uiremotenotificationtypesound | Uiremotenotificationtypealert)];

3).Add the following 2 methods (used to handle the Register remote notification with device token and register error events) in APPDELEGATE.M file
-(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);
4).Run the app in real device (simulator doesn ' t support push notification). Then you will see a popup window on the device (the window will only appear once, the reload app will not appear again), prompting you that the app send push notification to you, if you agree then clickOK, disagree then click "Now Allow ". If you choose OK, your app will be in the list in "Setting > Notifications". And then your Xcode output console will display your device token.

5 . Export "Pushdemo" private key to a ". P12" file (which will be used later to generate APNs provider. P12 or. pem file)
1). Right click "Pushdemo" Private key and select "Export ..." Pushdemo
2). Save the private key as "PUSHDEMOKEY.P12" file, click Save button
3. This will allow you to enter 2 times to encrypt the password for the. p12 file, such as "123321", and then you will be asked to enter a your Mac account password

6). The "Aps_development.cer" file generated by the "PUSHDEMOKEY.P12" file and step 6 generated in 5 is the source for APNs provider side, APNs provider side to push The "Cert + key" file used in the message is generated by the 2 file. This step is used to generate the for APNs provider side (PHP version) to use this "cert + key" Pem file.
1 open Terminal, go to Desktop (assuming that the 2 file is in Desktop)
2 Execute the following command to generate the Pem file "Pushdemocert.pem" corresponding to the APNs CER file

OpenSSL x509-in aps_development.cer-inform der-out Pushdemocert.pem

3 Execute the following command to generate the Pem file "Pushdemokey.pem" corresponding to the private key. P12 file (Note: The execution process will require you to enter the password that was set when "PUSHDEMOKEY.P12" was created, and set " Pushdemokey.pem "password)
OpenSSL pkcs12-nocerts-out pushdemokey.pem-in pushdemokey.p12

4) Execute the following command to make the key PEM file generated by step 11.2 generated by Cert PEM file and 11.3 as a PEM file "Pushdemock.pem"

Cat Pushdemocert.pem Pushdemokey.pem > Pushdemock.pem

7). The "Aps_development.cer" file generated by the "PUSHDEMOKEY.P12" file and step 6 generated in 5 is the source for APNs provider side, which is a simple test of whether the 2 files are valid
1 open Terminal, go to Desktop (assuming that the 2 file is in Desktop)
2 Execute the following command to test if you can connect Apple's provided unencrypted (that is, no certificate is required!). ), APNs server
telnet 2195

If you see the following output, it means your computer can connect APNs. If error occurs, check your firewall to allow outgoing connections on port 2195.
Trying ...
Connected to
Escape character is ' ^] '.
Press CTRL + C to close the connection.

3 Execute the following command to test the ability to connect the 2 PEM file! generated by the encrypted (use 2) and 3) supplied by Apple APNs "sandbox" server for development.

OpenSSL s_client-connect Pushdemocert.pem-key PushDemoKey.pem

During execution, you will be asked to enter the password that was set when PUSHDEMOKEY.PEM was generated. If connect server succeeds, will wait for you to enter the string, you can enter any string, then return, will disconnect server. If the connection is unsuccessful, OpenSSL displays an error message.
Note: There are actually 2 APNs servers:the "sandbox" servers (for testing) the live server (for production mode). What we're testing here is the sandbox server. The live APNs server operates similarly.

8). Build provider server side (PHP version)
1). Download Simplepush PHP code to your Mac machine and then it.
2. Remove the Pk.pem from the Simplepush folder (it is useless), the "Pushdemock.pem" copy Tosimplepush folder, generated by step 11.4
3). Modify the following lines of simplepush.php file:
//Put your device token (without spaces):
Device token from 4th of Step 10, obtained in output console, note: To remove the angle brackets before and after, and all the spaces in the middle

$deviceToken = ' 43fcc3cff12965bc45bf842bf9166fa60e8240c575d0aeb0bf395fb7ff86b465 ';

Put your private key ' s passphrase here:

The value is 3) the password set when the PUSHDEMOKEY.PEM is generated

$passphrase = ' 123456 ';

Put your alert:

$message = ' My i-push notification! ';

... stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', ' Pushdemock.pem ');

4. In terminal window, go to the Simplepush folder, and then execute the following command, your iphone should receive a push message.
PHP simplepush.php

This article has been organized into the "iOS push Tutorial", welcome to learn to read.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.