1. Basic knowledge of Push mode:
When we develop the applications that need to interact with the server, we basically need to get the server-side data, such as "earthquake emergency traffic" need to get the latest seismic information on the server. To get information on a server that is not regularly updated in general there are two methods, the first is the client uses pull (pulled), and then go to the server for a period of time to get information to see if there is updated information appears. The second is the way the server uses push (push), when there is new information on the server side, then push the latest information to the client.
While pull and push two ways to get server-side update information, it is clear that push is better than. Because the pull mode is more cost to the client's network traffic, the main is the charge power.
When developing Android and iphone apps, we often need to instantly push notifications from the server to the mobile phone client, with a simpler and perfect push notification solution on the iphone, and I'll cover the iphone in more detail. But the Android platform is relatively cumbersome to implement, and recently spent a few days on the Android push notification service for a preliminary study. On Android phone platform, Google provides C2DM (cloudto Device Messaging) service, at first I was ready to use this service to achieve the push function on my mobile phone.
Android Cloud to Device Messaging (C2DM) is a service that helps developers send data from a server to an Android application. The service provides a simple, lightweight mechanism that allows the server to notify the mobile application to communicate directly with the server to obtain application updates and user data from the server. The C2DM service is responsible for processing transactions such as Message Queuing and distributing these messages to applications running on the target device. About C2DM specific use process, I will later in the post in detail, here we first understand the approximate scenario.
C2DM Operation Process Diagram:
But a study has found that the service has a lot of problems:
1) The C2DM is built into Android's 2.2 system and cannot be compatible with the old 1.6 to 2.1 system;
2) C2DM need to rely on Google's official C2DM server, due to the domestic network environment, this service is often not available, if you want to use it well, our app server must also be abroad, this is probably not every developer can achieve; With the above two restrictions on use, which led me to finally give up the plan, but I would like to use another article to describe in detail the framework of the C2DM and the corresponding settings of the client and app server, can be used as a learning and reference. If C2DM does not meet our requirements, then we need to implement the communication protocol between the Android phone client and the app server to ensure that the Android device will receive it in time when the app server wants to send a message to the specified Android device.
2. Several common solutions
1) Polling (pull): The application should periodically connect with the server and query whether there are new messages arriving, you must implement the communication with the server, such as Message Queuing. And you also have to consider the frequency of polling, if too slow can cause some message delay, if too fast, it will consume a lot of network bandwidth and battery.
2) SMS (Push): On the Android platform, you can understand the server's intentions by intercepting SMS messages and parsing the message contents. This is a good idea and I've seen applications that use this scenario. The benefit of this scenario is that full real-time operations can be achieved. But the problem is that the cost of this solution is relatively high, and it's hard to find a free short message sending gateway about the implementation of this scenario.
3) Persistent connection (Push): This solution can solve the performance problems caused by polling, but still consumes the cell phone's battery. Apple's push service works well because each cell phone only maintains a connection to the server, and in fact C2DM works. However, there is a shortage of solutions, that is, it is difficult for us to achieve a reliable service on mobile phones.
The Android operating system allows you to kill system services in low memory situations, so your notification service is likely to be killed by the operating system. There are significant deficiencies in the first two programmes, and the third one is inadequate, but we can make up for it through good design, so that the programme can work effectively. After all, we need to know that both Gmail,gtalk and Googlevoice can be updated in real time.
3. MQTT protocol for Android push
Android push with the MQTT protocol is a lightweight message publishing/subscription protocol, It is the ideal solution for implementing a message Push server based on a mobile client. Wmqtt.jar is an implementation of the MQTT protocol provided by IBM. We can from Download the instance code of the project and find a server-side implementation
The schema is as follows:
Wmqtt.jar is an implementation of the MQTT protocol provided by IBM. We can from the following sites download it. You can add the jar package to your own Android app.
4.RSMB implementation push:
really Small Message Broker (RSMB), he is a simple MQTT agent , also provided by IBM. Open port 1883 by default, which is responsible for receiving messages from the server and forwarding them to the specified mobile device.
sam is a php Library We can download it from this.
send_mqtt.php is a PHP script that receives messages via post and sends messages to RSMB via Sam.
Really Small Message Broker (RSMB), he is a simple mqtt agent, also provided by IBM. Open port 1883 by default, which is responsible for receiving messages from the server and forwarding them to the specified mobile device.
5. XMPP protocol for Android push
This is the scenario that I used in the project. In fact, Google's official C2DM server is also packaged using the XMPP protocol. XMPP (Extensible Communication and Presentation Protocol) is an Extensible Markup Language (XML)-based protocol that is used for instant messaging (IM) and online probing. The agreement may eventually allow Internet users to send instant messages to anyone else on the Internet. About XMPP Protocol I have introduced in the last blog post, we can refer to:http://www.cnblogs.com/hanyonglu/archive/2012/03/04/2378956.html
ANDROIDPN is a Java open-source Android push notification implementation based on the XMPP protocol, and I'll introduce ANDROIDPN in detail later in blog post. It contains the full client and server side. Through the source code research I found that the server is basically in another open source project OpenFire based on the implementation of the modification, but the more depressed is the ANDROIDPN document is written in Korean, so the entire study process is basically read source.
The implementation intent is as follows:
The ANDROIDPN client needs a Java-based open source XMPP protocol package Asmack, which is also based on another open source project smack under OpenFire, but we don't need to compile it ourselves, The ANDROIDPN client can be used directly inside the Asmack.jar. The client uses the Xmppconnection class provided in Asmack to establish a persistent connection with the server, and the user registers and authenticates through the connection, as well as the notification sent by the server via this connection.
The ANDROIDPN server is also implemented in the Java language, based on the OpenFire Open source project, but its Web Part uses the spring framework, which is different from OpenFire. The ANDROIDPN server contains two parts, one is the XMPP service that listens on 5222 ports, is responsible for communicating with the client's Xmppconnection class, the role is user registration and authentication, and sends a push notification message. The other part is the Web server, which uses a lightweight HTTP server that is responsible for receiving Web requests from users. The server architecture is as follows:
The top layer contains four components, namely Sessionmanager,auth Manager,presencemanager and notification Manager. SessionManager is responsible for managing the client-server session, Auth Manager is responsible for client user authentication management, and Presence Manager is responsible for managing the client user's login status. The Notificationmanager is responsible for implementing the server push message function to the client.
The biggest advantage of this solution is simplicity, and we don't have to rely on OS versions like C2DM, or worry about a day when Google servers aren't available. With the XMPP protocol, we can further extend the protocol to achieve a more complete function. With this scheme, we can only send text messages at this time, but generally enough for push, because we can not expect to push to get all the data, in general, the use of push just tell the phone server has some changes, when the client receives the notification, should actively to the server to obtain the latest data, This is the complete implementation of the push service.