Using ANDROIDPN to achieve push

Source: Internet
Author: User
Tags closing tag manage connection server port android push notification

About server-side push to Android clients, there are three main ways: polling, the application should be phased with the server to connect and query whether there is a new message arrives, you have to implement the communication between 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; SMS (by sending text messages and parsing text messages to get server-side instructions), the problem is that it is difficult to find a free gateway to send text messages; Finally, a persistent connection Mainly socket communication, this solves the performance problem, but the power consumption problem still can not solve.
Here, we mainly introduce the third type, using a persistent connection to push the way. Now more mature timely messaging protocol total of four, and undoubtedly the most mainstream is the XMPP protocol, it is an XML-based delivery protocol, with strong flexibility and scalability. It is characterized by the transfer of complexity from the client to the server side. In the online can find a lot of XMPP data, here is not to repeat, or more pull. In summary, the main notable advantages of XMPP are the following:

1, distributed anyone can run their own XMPP server, it does not have a primary server

2, security is very high. Reliable security using technologies such as SASL and TLS

3. It is open source, easy to learn and understand

4, cross-platform undoubtedly, the use of XML for the transmission of

To finish the pros, we have to get to the point that Java development based on the XMPP protocol has an open source framework, which is smack, which mainly encapsulates some implementations of XMPP. And if it is not possible to use it directly on Android, because Android is missing some Java library, so an improved version of the Asmack was born, it is specifically for Android and improved Android smack. Another open source framework is the emergence of a reference smack on the basis of the implementation and server-side persistent connection to achieve server-to-client push, that is, Android push notification, referred to as ANDROIDPN.

The ANDROIDPN integrates asmack on the client. This makes it easy to simply set up an XMPP protocol-based socket connection to the server side. In ANDROIDPN's client, the class that manages the connection is Xmppmanager, which is used primarily to manage connection information, such as XMPP ports, IP, logged-in User name passwords, and maintenance of connections. Why is there a user name and password? This has to mention the specifics of XMPP. The whole server and client communication is based on a session process, the session begins, first specify the server port number, and then send the above mentioned information to the server side, how to send the message? The delivery is initiated as a root node, and its closing tag is sent only when the server and client are closed. The client uses the XMPP protocol only to receive messages, and all other operations are given to the server, such as administrative connections, message storage, and so on, which greatly reduces the burden on the client. So how is the client and server-side message response implemented? To identify by an ID, the details can be viewed in the XMPP protocol.

Once the binding is registered, the server is connected to the client and the client is only responsible for receiving the message. So when we apply ANDROIDPN, the client is very simple. And on the server side, what did ANDROIDPN do?

In terms of server-side presentation, the main technology used by ANDROIDPN is spring and hibernate. It is mainly used for displaying user status and sending information, such as:

This aspect of the technology has been relatively mature, it is no longer elaborate, mainly to say is the management of XMPP. In the server side of the source code in a Org.androidpn.server.xmpp.net.Connection class, mainly represents a server XMPP connection, note is only one, it can ensure that when the server shuts down, send a token to the client, tell the connection is disconnected, need to reconnect.

Org.androidpn.server.xmpp.session.SessionManager the primary user to manage all sessions, such as disconnect, delete session and establish connection, add session, etc.

While managing the socket connection, ANDROIDPN adopted the Mina framework for management, Mina the advantage is to change our traditional way of managing sockets, such as not set up a socket to open a thread, and Mina can implement multiple threads to manage n multiple users. There is no doubt that there is a huge benefit in dealing with high-concurrency pushes.

Reasonable use of the listener to manage the session, but also the advantages of ANDROIDPN. In terms of security, the TLS (Secure Transport Layer) policy has been developed, and security authentication has been adopted, all of which are good.

Of course, the inevitable 30-second heartbeat package is still essential.

In summary, the benefits of using ANDROIDPN are as follows: the use of a fully open XMPP protocol for data transfer (Qq,msn,gtalk, etc.); Good framework support (for Android-generated push frame asmack, and good management socket framework Mina, are very mature products); Fully open source code (we can modify it on a ANDROIDPN basis to meet any of our requirements changes), greatly reducing the client's code, reducing the difficulty of Android development. The disadvantage is self-evident, the use of too many frameworks, if you want to change some specific implementation, may be migrated to start the whole body. But if you're a lazy man, you're completely satisfied with your needs.

Finally, I tidied up the source of the ANDROIDPN, can be used completely. The original server side of the jetty framework to replace Tomcat, I put it back into the tomcat, can be perfect and your server-side fusion. Lazy people, come on because the file is too large, so you use the outside of the company like to download it. Only need to change the client's raw folder under the IP address can be run:

Using ANDROIDPN to achieve push

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.