Easily handle IOS remote message push

1、 Introduction there are two ways to push messages in IOS: local push and remote push https://www.oudahe.com/p/16690/ There is a detailed introduction in this blog, which mainly discusses the process and configuration of remote push.

2、 Principle of remote push mechanism

1. Starting from a very popular diagram, you can always see the following flow diagram when searching IOS remote push. Because this diagram is really popular, I also quote it here:

The diagram is very clear. The general meaning is as follows: your application server sends the message to Apple's APNs server, which pushes the message to the specified iPhone, and finally the iPhone is responsible for pushing the message to your app. Not to mention how this process is implemented, just looking at this process, you may think that adding an apple APNs between your server and client will not increase the burden on developers? In fact, the result is just the opposite, because Apple's unified management of push makes our developers' work extremely simple.

2. How does the server connect to the client

If you are an Android Developer, you must know a lot about long links and heartbeat packages. In fact, most Android applications push through long links. Because of the openness of the Android system, app can easily achieve self startup and background long link, and heartbeat verification is to always ensure that the long link is connected, and then the server directly pushes messages. If IOS developers also adopt this idea, it will be very difficult. I can only say that it is too difficult to keep an app service in IOS from being killed by the system. Through the above flow chart and comparing the push ideas of Android, we can easily understand that there is always a long link in IOS, that is, the system itself. This long link is always connected to the APNs server, and then uniformly manages the push of all applications.

3. Is this the advantage of IOS push mechanism?

The following are just my personal views. The system has no advantages or disadvantages. The advantages or disadvantages lie in personal preferences.

1. Because the push server is the authenticated user of appleid, the push reliability will be high.

2. All push messages are uniformly managed by APNs with high efficiency.

3. The system only needs to maintain a long link on the client, which saves user traffic consumption and mobile phone performance consumption, improves security, and reduces the probability of malicious push and rogue software.

3、 Let your app receive remote push every minute

1. If a worker wants to do something well, he must first use his tools -- create a push Certificate (1) request CSR file

Find the keychain access in the MAC application and open it.

Click the key chain in the option bar to access the certificate assistant in the:

Choose to request a certificate from a certification authority:

Fill in the email and name, select Save to disk, and then continue.

At this time, we have such a file in the storage place: certificatesigningrequest certSigningRequest。

(2) Export key file

Open the key chain and you will find a pair of keys. The name is the common name you fill in above.

We select the private key for export, and then set our own password:

At this time, we have another suffix named P12 file.

(3) Create appid

reach https://developer.apple.com Member center:

After logging in with the paid developer appleid, select certificates:

If your project has created an app ID, you can not re create it, but the app ID you create must support remote push. If not, click the plus sign to create one:

In the subsequent interface, there are two types of APP ID: explicit and wildcard, which are special and universal respectively. We need the push function. This ID cannot be universal, so we choose the first one.

The bundle ID to be filled in here must be consistent with that in our app:

In the service settings of APP ID, check push notification and click continue. Push IOS remote messages in minutes

Then click Submit, and finally click done. At this time, the app ID we just created will appear in our app IDS list.

(4) Create certificate

Click the app ID we just created, and you will see that the push notification behavior is not set. We click Edit.

In the push notifications setting, the following interface is displayed. Development is the development certificate and production is the product certificate. We now need to test, so we use the development certificate and the production certificate when going online. Click Create certificate.

Then click continue. The following interface will let us select a CSR file. The file we created in the first step is useful here. Select the file and click generate.

Download the created certificate to your computer:

So far, we have three files, namely CSR file P12 file CER file. Put these three files in the same directory CER files are divided into test and product files, which you need to choose by yourself. After writing so much, we have finished our preparatory work. Don't lose heart. In fact, your push work is basically finished. It's just that the application process is a little troublesome, but we hardly need to configure the engineering code.

2. The soldiers and horses are not moving, and the food and grass are first - the setting of information push at the server

(1) Processing certificate

Open the terminal CD to the directory where the three files we got above are located.

Execute the following commands at the terminal:

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

aps_ development. CER is just generated The file name of the CER file. A PEM file will be generated in the current folder, which is the corresponding certificate of our server.

Then execute the following command:

$ openssl pkcs12 -nocerts -out PushKey. pem -in key. p12

key. P12 is generated above The file name of the p12 file. At this time, the terminal will let you enter the password. The password here is the password of the key we set above. Enter the password and press enter. If the password is correct, we will be asked to enter the new password (remember). After entering it twice, the terminal will prompt that the pushkey has been successfully created PEM file.

The last step is to combine the two PEM files we generated into one:

$ cat PushCert. pem PushKey. pem > ck. pem

(2) Is the test certificate available

Execute the following command at the terminal:

$ telnet gateway. sand@R_786_2419 @.push. apple. com 2195

Wait a minute. If the terminal displays the following situation, the certificate is normal.

Then execute the following command:

openssl s_ client -connect gateway. sand@R_786_2419 @.push. apple. com:2195 -cert PushChatCert. pem -key PushKey. After PEM enters the password, press enter to display the following results, and the connection is successful:

3. The ends of the earth, one step away - configuration in applications

Add the following code in appdelegate of our project:

The following is the code of PHP server searched online:

Put the above PHP file in the same directory as our CK file. In the current directory of the terminal, execute the following commands:

$PHP push. PHP

If our device is slightly normal, we can receive the push message:

4、 Several points of attention

1. If the terminal prompts an error such as inaccessible key when sending information, please check whether the CD is to the current directory. If there is still a problem, regenerate the key part again.

2. Note that the characters in the PHP code are English characters.

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
分享
二维码
< <上一篇
下一篇>>