Android Message Push XMPP protocol

Source: Internet
Author: User
Tags android push notification

1. Introduction

The so-called message push is from the server side to the mobile terminal to send connections, transmission of certain information. For example, some news clients receive one or more notifications at regular intervals, which is a push message from the server side, as well as some commonly used IM software such as, gtalk, etc., all with server push function.

The push method is as follows:

1) communication between the server side and the client via SMS.

On the Android platform, you can achieve full real-time operation by intercepting SMS messages and parsing the message content to understand the server's intentions. But the problem is that the cost of the solution is relatively high and depends on the operator.

2) cyclic Active timing acquisition

This method requires the client to do a timed or periodic access to the server-side interface to get the latest messages. Polling too slowly can cause some messages to be delayed too quickly and consume network bandwidth and battery too much.

3) Persistent Connection

This solution can solve the performance problems caused by polling, but it still consumes the cell phone's battery. We need to open a service to maintain a persistent connection with the server (Apple and Google's C2DM is this mechanism). However, for Android, when the system has low resources available, the system will force the shutdown of our service or the application, in which case the connection will be forced to break. (Apple's push service works well because each phone only maintains a connection to the server, and in fact C2DM works the same way.) That is, all the push services are done through a proxy server, in which case only a single server is required to maintain a persistent connection. c2dm=Cloud to Device Messaging).

The third is, in contrast, the most feasible. Write system services or start-up functions for the software, or if the system resources are low, the service can be shut down and then restarted in the OnDestroy () method to achieve a durable connection.

The C2DM is built into Android's 2.2 system and is not compatible with the old 1.6 to 2.1 system, and relies on Google's officially provided C2DM server, which is often unavailable due to the domestic network environment.

The XMPP protocol, which is built on the TCP protocol, not only provides the capability of this kind of persistent connection, can realize the duplex communication between server and client, but also can not rely on the limitation of the system version and Google Server, which provides a better solution.

2. XMPP protocol

XMPP full name extensible Messaging and Presence Protocol, formerly known as the Jabber Project, is an XML-based open Instant Messaging protocol. XMPP by Google Talk and NetEase bubble application by the vast number of netizens contact. The key features of XMPP are decentralized instant messaging systems, and the use of XML streaming. XMPP is currently being standardized by the IETF International Standards Organization.

  Android Push Notification (ANDROIDPN) is a Java open source implementation based on the XMPP protocol, which contains the complete client and server side. The server side is basically modified on the basis of another open source project OpenFire.

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. These two ways of the server are significant: when the corresponding TCP port is closed by a firewall, it can be accessed using polling, and therefore helps through the firewall.


3. XMPP protocol for Android push

This paper mainly introduces the process of using ANDROIDPN project to implement push.


For information push in Android, one of the more sophisticated solutions is to use the XMPP protocol. The ANDROIDPN project is an open source project that uses the XMPP protocol for information push.

Features of the Apndroid Push notification:

Fast integration: Provides a faster way to use than C2DM and avoids various restrictions.

No server Setup required: Reduce the additional server burden by using cloud services.

Can push messages to the site page at the same time, Android phone

Less power consumption, less traffic.

Specific configuration process:

First, we need to download Androidpn-client-0.5.0.zip and Androidpn-server-0.5.0-bin.zip.

: http://sourceforge.net/projects/androidpn/(If you can't open the above URL, you need to turn over the wall, you know).

Extract two packages, Eclipse Import client, configure target platform, open raw/androidpn.properties file, configure client program.

1. If it is the emulator to run the client program, the xmpphost configured to 10.0.2.2[simulator 10.0.2.2 think is the address of the host, 127.0.0.1 is the emulator itself loopback address, 10.0.2.1 represents the gateway address, 10.0.2.3 represents the DNS address, 10.0.2.15 represents the network address of the target device], for more information about the simulator, you can refer to the relevant information, not detailed here.

Xmppport=5222 is the server's XMPP service listening portRun Androidpn-server-0.5.0\bin\run.bat boot server, access http://127.0.0.1:7070/index.do from the browser (ANDROIDPN server has a lightweight Web server, On 7070-Port listening requests, accept user-entered text messages)

running the client, the client initiates a connection request to the server, and after successful registration, the server can identify the client and maintain a long IP connection with the client.


2. If you are testing the simulator of other machines in the same LAN (or using the same wireless router's WiFi connection), you need to set this value to the server machine's LAN IP.

For example, your computer and Android phone are connected to the same wireless router WiFi, the computer's IP address is 192.168.1.2 and the IP address of the phone is 192.168.1.3, this time need to change this value to xmpphost=192.168.1.1 or power The IP address of the brain, it can be used on the phone.

3. If the test is not on the same LAN, we need to set this value to the IP address of the server.

the specific configuration is as follows:



       

My Computer IP is: 192.168.8.107

The server runs the main interface:

Push message:


test results such as:


Simulator and real machine test pass.

Android Message Push XMPP protocol

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.