Developing Android and iPhone applicationsProgramWe often need to push all kinds of notifications to the mobile client from the server.
Message notification
There are two solutions:
1 round robin (poll)
Applications should be connected to the server in stages and check whether new messages arrive. You must implement communication with the server, such as Message Queuing. In addition, you also need to consider the polling frequency. If it is too slow, it may lead to delay of some messages. If it is too fast, it will consume a lot of network bandwidth and battery.
However, polling once every five minutes will consume 10% of the power of the phone in the long term.
2 push
A) SMS. InAndroidOn the platform, you can interceptSMSMessage and parse the message content to understand the intention of the server. The server uses a specific number to send commands to a specific device. The device performs the operation according to the Command received. However, the problem is that the cost of this solution is relatively high, and it is difficult for you to find a free short message sending gateway.
B) persistent connection ). This solution can solve the performance problem caused by polling, but it will still consume the cell phone's battery.AndroidThe operating system maintains a persistent connection. If the connection is maintained, the power consumption is relatively small and the power consumption is reduced. For details, see Android c2dm presentation at O 'Reilly androidopen conference.
Android 2.2 and later versions have added the cloud to device messaging (c2dm) framework and supported the push function in the system. It is easier to use the push function based on the Android platform. Although c2dm is still in the lab stage, small-scale usage should be fine.
C2dm framework
The c2dm function of Android has the following requirements:
1. android2.2 or a later version is required.
2. Google accounts must be configured on Android devices that use the c2dm function.
3. register the user email account that uses the c2dm function here (it is best to register a separate Gmail email account for c2dm ).
Let's take a complete c2dm process. Here we will use the chrome to phone process diagram officially launched by Google to describe it.
Figure 1 c2dm operation procedure Diagram
To use c2dm for push, follow these six steps:
(1) Registration: the Android device uses c2dm function of the user account (such as android.c2dm.demo@gmail.com) and app name sent to c2dm server.
(2) The c2dm server returns a registration_id value to the Android device. The device needs to save this registration_id value.
(3) The Android device sends the obtained registration_id and c2dm functions of the user account (android.c2dm.demo@gmail.com) to their own server, but the general user account information is determined by the server, so do not have to send.
In this way, the Android device registers the c2dm function, and then receives the messages pushed by the c2dm server.
(4) The server obtains data. The example in the figure below shows chrome to phone. The server receives the data sent by the Chrome browser. Data can also be generated locally on the server. The server here is Google appengine (a good service, but it is a pity that it has been screen-up in China). You need to replace it with your own server. The server also gets the clientlogin permission auth for registering a user account (android.c2dm.demo@gmail.com) that uses the c2dm feature.
(5) the server sends the data to be sent together with the registration_id and carries the obtained auth in the header to the c2dm server in post mode.
(6) The c2dm server sends data to the corresponding Android device in Push mode. The Android device only needs to obtain data from the corresponding key in the program in the format previously discussed with the server.
Analysis of c2dm andCodeFor example, see: Android cloud to device messaging (c2dm)-tutorial
Self-implemented push mechanism
XMPP is a protocol based on the Extensible Markup Language (XML). It is used for instant messaging (IM) and online detection. In fact, Google's official c2dm server uses the XMPP protocol for encapsulation at the underlying layer.
For how to implement it by yourself, see the android push notification guide.