Android message pushing Xmpp protocol

Source: Internet
Author: User
Tags android push notification

Android message pushing Xmpp protocol

1. Introduction

Message pushing means sending a connection from the server to the mobile terminal and transmitting certain information. For example, some news clients receive one or more notifications at intervals, which is the push messages sent from the server, and some common IM software such as GTalk, all have the server push function.

The push method is as follows:

1) Communicate with the client through SMS.

On the Android platform, you can intercept SMS messages and parse the message content to understand the intention of the server and perform complete real-time operations. However, the problem is that the cost of this solution is relatively high and dependent on operators.

2) Cyclic active timing acquisition

This method requires the client to make a scheduled or periodic access to the server interface to obtain the latest message. Too slow polling frequency may lead to delay of some messages. Too fast will consume a lot of network bandwidth and battery.

3) persistent connection

This solution can solve the performance problem caused by polling, but it will still consume the cell phone's battery. We need to establish a service to maintain persistent connection with the server (Apple and Google's C2DM are like this ). However, for Android systems, when the system has low available resources, the system will forcibly disable our services or applications. In this case, the connection will be forcibly interrupted. (Apple's push service works very well because each mobile phone only maintains a connection with the server. In fact, C2DM works like this. That is, all the push services are completed through a proxy server. In this case, you only need to maintain a persistent connection with a server. C2DM =Cloud to Device Messaging).

In contrast, the third method is the most feasible. Write a system service for the software or enable the startup function. If the system resources are low, you can restart the service in the onDestroy () method after the service is disabled to achieve persistent connection.

C2DM is built into the Android 2.2 System and cannot be compatible with the old 1.6 to 2.1 systems. It depends on the C2DM server officially provided by Google. Due to the domestic network environment, this service is often unavailable.

The XMPP protocol established on TCP not only provides the persistent connection function, but also enables duplex communication between servers and clients, and does not depend on the system version or google server, provides a better solution.

2. XMPP protocol

XMPP stands for Extensible Messaging and Presence Protocol, formerly the Jabber project. It is an XML-based open instant Messaging Protocol. XMPP is exposed by many Internet users due to Google Talk and Netease bubble applications. A key feature of XMPP is its Distributed Instant Messaging System and XML streaming. XMPP has been standardized by IETF International Standards Organization.

  Android push notification(Androidpn) is an open-source java implementation based on XMPP protocol. It contains the complete client and server. This server is basically modified and implemented on the basis of another open source project openfire.

The androidpn client needs to use a java-based open-source XMPP protocol package asmack. This package is also based on the openfire's other open-source project smack, but we do not need to compile it ourselves, you can directly mount the asmack in the androidpn client. jar. The client uses the XMPPConnection class provided in asmack to establish a persistent connection with the server and use this connection for user registration and logon authentication. This connection also receives notifications sent by the server.

The androidpn server is also implemented in java. It is based on the openfire open source project. However, its Web part adopts the spring framework, which is different from openfire. The Androidpn server consists of two parts. One is the XMPP service listening on port 5222, which is responsible for communicating with the XMPPConnection class of the client. It serves as a user registration and identity authentication, and sends push notification messages. In addition, a Web server uses a lightweight HTTP server to receive users' Web requests. The two methods of the server are of extraordinary significance: when the corresponding TCP port is closed by the firewall, you can use the round-robin Method for access, and thus help through the firewall.


3. XMPP protocol for Android push

This article describes how to use the AndroidPn project to implement push.


Implement information push in Android. One of the more mature solutions is to use the XMPP protocol. The AndroidPn project is an open-source project that uses the XMPP protocol to push information.

Features of Apndroid Push Notification:

Quick integration: provides a faster way to use than C2DM, avoiding various restrictions.

No Server SETUP required: reduces the extra server load by using cloud services.

Push messages to the website page at the same time, android mobile phone

Low Power Consumption and low traffic consumption.

Specific configuration process:

First, download androidpn-client-0.5.0.zipand androidpn-server-0.5.0-bin.zip.

: Http://sourceforge.net/projects/androidpn/ (if you can not open the above URL, it needs to flip the wall, you know ).

Decompress the two packages, import Eclipse to the client, configure the target platform, open the raw/androidpn. properties file, and configure the client program.

1. if a simulator is used to run the client program, configure xmppHost to 10.0.2.2. [The simulator considers 10.0.2.2 as the address of the host, 127.0.0.1 as the loopback address of the simulator, and 10.0.2.1 as the gateway address, 10.0.2.3 indicates the DNS address, and 10.0.2.15 indicates the network address of the target device.] For more information about simulators, see related information.

XmppPort = 5222 is the server's xmpp service listening port running androidpn-server-0.5.0 \ bin \ run. bat starts the server and accesses http: // 127.0.0.1: 7070/index from the browser. do (androidPN Server has a lightweight web Server that listens to requests on port 7070 and receives text messages from users)

When the client runs, the client initiates a connection request to the server. After successful registration, the server can identify the client and maintain a persistent connection with the client's IP address.


2. if it is a simulator test of another machine in the same LAN (or a real machine using the same wireless router wifi), you need to set this value to the LAN ip address of the server machine.

For example, your computer and android mobile phone both use the same wireless router wifi to access the Internet. The IP address of the computer is 192.168.1.2, And the IP address of the mobile phone is 192.168.1.3, in this case, you need to change the value to xmppHost = 192.168.1.1 or the IP address of your computer to use it on your mobile phone.

3. If it is a real machine test that is not in the same LAN, we need to set this value to the IP address of the server.

The specific configuration is shown in:




My computer IP address is 192.168.8.107

Main server running interface:

Push messages:


The test result is as follows:

<喎?"http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + CiAgICA8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20141106/2014110608125645.png" alt = "">


The simulator and the real machine pass the test.

Related Article

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.