First, let's briefly introduce three main methods of Android message push. If you have read similar articles, ignore these three methods.
1. Use the SMS service, that is, the server sends a text message, and then the mobile client listens to the broadcast of the text message, and then processes the data to push the message. The advantage is power saving, saving traffic, but the operator will be very expensive. After all, sending text messages requires money and environmental restrictions. When a tablet or user is down, there is no way to use push. Therefore, this solution is only available in certain and special circumstances (such as mobile, Unicom, and telecom.
2. Actively retrieve data from the network using the round-robin method. Electricity fees and traffic fees. This method is easy to understand and easy to implement (our almost Client 1.0 is the way to implement push ). It can still be used if it is just a Demo. However, as a running application, no matter how you optimize it, it usually consumes traffic. After all, you have been obtaining data from the network.
3. Persistent connections are used. Generally, the main data pushed depends on this method for data push. Saves traffic and power consumption. The principle is to establish a persistent data stream connection with the server. The mobile client is always waiting for data from the mobile client. Because the connection is continuous and there is no data flow operation, the traffic in the network is relatively low. However, because the network connection is always maintained, such message push is definitely expensive. Many software are created in this way. C2DM is recommended for Apple and Android. (Apple only uses a connection for the entire mobile phone, which Android originally wanted to use, however, Google's server is located in the United States, and there is a problem with the tianchao firewall. Such push will be unstable ). However, this mode also has some drawbacks. In the case of unstable networks (trains, buses, and driving may cause unstable networks), the Socket is easier to disconnect. When the connection is unstable. Pushing data is easy to fail. There are still many push components based on this mode.
Next, let's briefly introduce the planning of message pushing on the client side. In almost the client, there are not many functional modules applied to push, including private messages, notifications, requests, and instant chat functions (in planning ). Among them, private messages, notifications, and requests are non-instant requests, and a simple delay of several minutes is not very relevant (for example, your classmates AT you on the site, you didn't have much trouble dealing with this action five minutes later.) instant chat is a real-time function (think about it, your wife told you, miss you, it takes 20 minutes for you to reply ,......). These two requirements are completely different. This time we will only introduce the previous one.
This section describes how to process mobile clients and servers.
The mobile client first needs to ask the server whether to allow Socket connection. It is very simple to not allow data processing. It directly uses the round-robin method to obtain data. If the server allows connection, try the connection and check whether the Socket is available. If the network is unstable for a long time, it performs lateral Round Robin and checks whether the network environment is stable. When the network is stable, use Socket for data push.
The server first checks whether Socket is enabled. Wait for the mobile phone to connect to the client and then push the data to the mobile phone.
In this way, the data can be pushed to the mobile client normally.
The first write. Poor writing. Welcome to the bricks.