Implementation and principle analysis of message push based on IOS

1、 Message push principle:

Before implementing message push, several concepts related to push are mentioned, as shown in Figure 1-1:

1. Provider: the server that provides push for the specified IOS device application (if the IOS device application is a client, the provider can be understood as the server [message initiator]);

2. APNs: Apple Push Notification Service;

3. IPhone: used to receive messages sent by APNs;

4. Client app: the application on the IOS device, which is used to receive the message sent by the iPhone APNs to a specified client app [the final responder of the message];

The above figure can be divided into three stages:

Phase 1: the provider packages the message to be sent and the destination IOS device ID and sends it to APNs;

Phase 2: APNs finds the IOS device with corresponding identification in the IOS device list of its registered push service, and sends the message to the IOS device;

Phase 3: the IOS device passes the sent message to the corresponding application, and pops up the push notification according to the settings.

The specific process is shown in Figure 1-2 below:

1. [client app] registration message push;

2. [client app] and [APNs service] need devicetoken, and the client app receives devicetoken;

3. [client app] sends devicetoken to [provider] push server program;

4. When the push server program meets the conditions for sending messages, [provider] sends messages to [APNs service];

5. [APNs service] sends a message to [client app]

2、 Message push implementation:

1. Generate * Certsigningrequest file, the steps are as follows:

[MacBook application - Utilities - Keychain access - Certificate assistant - Request certificate from certificate authority - certificate information (user email address {fill in your email address, such as: your@email.com }, the common name is {any, such as pushdemo}. The request is: {radio, select 'store to disk'}) - continue - save]. At this time, the file you specify will be generated in the place you specify. The default is certificatesigningrequest Certsigningrequest file, named pushdemo certSigningRequest. Here * Certsigningrequest has been generated. The specific operation steps are shown in the figure.

If the generation is successful, the and *. Will be listed in the [key string access | login | key] column The key associated with certsigningrequest. Here is pushdemo, as shown in the figure:

2. Create an app ID (configured in Apple Developer account)

(1) Log in to IOS Dev Center. After successful login, click (corresponding link of IOS provisioning portal), as shown in the figure:

(2) Create a 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 the bound app ID prefix, such as generate new}, bundle identifier (APP ID suffix) {fill in the bound app ID suffix, such as com. Yourcorp. Ishop}), as shown in the following figure:

The following record will be generated, as shown in the figure:

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

(4) Generate a certificate signing request [click 2configure à continue à step 1 in 2-7 to generate the * certsigningrequest file (here is ishop. Certsigningrequest) - generate - download it after generation, name it aps_developer. Cer], and double-click the aps_developer.cer certificate {associate the certificate with the key and import the certificate into MacBook}, as shown in the following figure:

(5) Create development provisioning profiles (provisioning | development | new profile). The specific operation process is shown in the following figure:

Click submit in the figure to generate the development provisioning profiles. Here is ishodevprofile Mobileprovision is shown in the figure below:

Download this development license certificate (for online debugging).

In summary, so far, we have generated: A: * Certsigningrequest file (used in step (4) to generate certificate b), B: APS_ developer_ identity. CER Certificate (used in the provider [push server] server application), C: *. Mobileprovision development license configuration file (used for online debugging in the client app client application).

3. Create a new project

1). Create a "single view application" project to save trouble, The "company identifier" + "production" you set must be consistent with the "bundle identifier" of the app ID created in step 5. 2). In the "didfinishlaunchingwithoptions" method of appdelegate. M file, add the following code (used to push notification feature for app register) / / let the device know we want to receive push notifications[ [UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

3). In appdelegate Add the following two methods (to handle register remote notification with device token and register error events) - (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). At this time, you will see a pop window on the device (the window will only appear once, and the reinstalled app will not appear again), prompting you that the app will send you a push notification. If you agree, click OK. If you disagree, click "now allow". If you select OK, click "setting > notifications" "There will be your app in the list, and your Xcode output console will display your device token.

5). Export "pushdemo" private key to a ". P12" file (this file will be used later when generating. P12 or. PEM file of APNs provider) 1). Right click "pushdemo" private key and select "export..." pushdemo 2). Save the private key as "pushdemokey. P12" file, click Save button 3). At this time, you will be asked to enter the password used to encrypt the. P12 file twice, for example " 123321 ", and then you will be asked to enter your Mac account password once

6). The "pushdemokey. P12" file generated in step 5 and the "aps_development. Cer" file generated in step 6 are the source files for the APNs provider side. The "cert + key" files used by the APNs provider side to push messages are generated through these two files. This step is used to generate the "cert + key" PEM file for APNs provider side (PHP version) 1) Open terminal, go to desktop (assuming the two files are in the desktop) 2) execute the following command to generate the PEM file "pushdemocert. PEM" corresponding to APNs cer file

openssl x509 -in aps_ development. cer -inform der -out PushDemoCert. pem

3) Execute the following commands to generate and private keys PEM file "pushdemokey. PEM" corresponding to p12 file (Note: the execution process will require you to enter the password set when "pushdemokey. P12" is created and the password set for "pushdemokey. PEM") OpenSSL pkcs12 - nocerts - out pushdemokey.pem - in pushdemokey.p12

4) Execute the following command to combine the cert PEM file generated by step 11.2 and the key PEM file generated by step 11.3 into a PEM file "pushdemo. PEM"

cat PushDemoCert. pem PushDemoKey. pem > PushDemoCK. pem

7). The "pushdemokey. P12" file generated in step 5 and the "aps_development. Cer" file generated in step 6 are the source files for the APNs provider side. This step is a simple test to see if the two files are valid. 1) open terminal, Go to desktop (assuming the two files are in the desktop) 2) execute the following command to test whether the APNs server telnet gateway provided by Apple can be connected without encryption (i.e. no certificate is required!) sand@R_481_2419 @.push. apple. com 2195

If you see the following output, your computer can connect APNs If an error occurs, check whether your firewall allows outgoing connections on port 2195. Trying 17.172. 233.65... Connected to gateway. sand@R_481_2419 @.push-apple. com. akadns. net. Escape character is '^]'. Press Ctrl+C to close the connection.

3) Execute the following command to test whether you can connect the APNs of the encrypted (2) and 3) PEM files!) provided by apple“ sand@R_481_2419 @“ server for development.

openssl s_ client -connect gateway. sand@R_481_2419 @.push. apple. com:2195 -cert PushDemoCert. pem -key PushDemoKey. pem

During execution, you will be asked to enter pushdemokey Password set when PEM is generated. If connect server succeeds, it will wait for you to enter a string. You can enter any string and press enter to disconnect server. If the connection is unsuccessful, OpenSSL displays an error message. Note: there are actually two APNs servers: the“ sand@R_481_2419 @"Server (for testing) the live server (for production mode) sand@R_481_2419 @ server。 The operation of live APNs server is similar.

8). Create provider server side (PHP version) 1) Download SimplePush PHP code to your mac machine and then unzip it. 2). Remove PK in simplepush folder PEM (it's useless). Copy "pushdemo. PEM" generated in step 11.4 to simplepush folder 3). Modify the following lines of simplepush.php file: / / put your device token here (without spaces): / / device token comes from point 4 of step 10 and is obtained in the output console. Note: remove the angle brackets before and after and all spaces in the middle

$deviceToken = '43fcc3cff12965bc45bf842bf9166fa60e8240c575d0aeb0bf395fb7ff86b465';

// Put your private key's passphrase here:

//This value is 3) generate pushdemokey Password set when PEM

$passphrase = '123456';

// Put your alert message here:

$message = 'My first push notification!';

//..... stream_ context_ set_ option($ctx,'ssl','local_cert','PushDemoCK.pem');

4). In the terminal window, go to the simple push folder, and then execute the following command. Your iPhone should receive a push message. PHP simplepush. PHP

This article has been sorted into IOS push tutorial. You are welcome to learn and read it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>