Android push using XMPP protocol

Source: Internet
Author: User
Tags android push notification

Http://fqj183-8041380.blog.163.com/blog/static/39955797201301052742170/

Hope everyone can study and help each other.

When developing Android and iPhone applications, we often need to push various notification messages from the server to the mobile client in real time, the iPhone already has a relatively simple and perfect push notification solution, which can be implemented on the Android platform but is relatively troublesome, recently, we have made a preliminary research on the android push notification service.

 

On the Android mobile platform, Google provides the c2dm (cloudto device messaging) service. At first, I was preparing to use this service to implement the push function on Android mobile phones.

Android cloud to device messaging (c2dm) is a service that helps developers send data to Android applications from servers. The Service provides a simple and lightweight mechanism that allows the server to notify mobile applications to communicate directly with the server, so that the server can obtain application updates and user data. The c2dm service processes transactions such as Message Queuing and distributes these messages to applications running on the target device.

However, this service has a major problem:

1. c2dm is built into the Android 2.2 System and cannot be compatible with the old 1.5 to 2.1 systems;

2. c2dm depends on the c2dm server officially provided by Google. Due to the domestic network environment, this service is often unavailable. If you want to use it well, our app server must also be abroad. This may not be implemented by every developer or company.

So I finally gave up this solution. Android push is implemented using the XMPP protocol. In fact, Google's official c2dm server uses the XMPP protocol as the underlying layer.

XMPP is a protocol based on the Extensible Markup Language (XML). It is used for instant messaging (IM) and online detection. This protocol may eventually allow Internet users to send instant messages to anyone else on the Internet.

Androidpn is an open-source Java Android push notification implementation based on XMPP protocol. It contains the complete client and server.

 

The implementation of androidpn is as follows:

 

The androidpn client needs to use a Java-based open-source XMPP protocol package asmack, which is also an open-source project smack Based on openfire,
However, we do not need to compile it ourselves. We can use asmack. jar in the androidpn client directly. 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. Based on the openfire open source project, its Web part adopts the Spring framework. 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. The other part is the Web server, which uses a lightweight HTTP server to receive users' Web requests. The server architecture is as follows:

The top layer includes four components: sessionmanager, auth manager, presencemanager, and notification manager. Sessionmanager is responsible for managing sessions between the client and the server. Auth manager is responsible for client user authentication management, presence manager is responsible for managing the logon status of client users, and icationicationmanager is responsible for pushing messages from the server to the client.

The server interface is as follows, which corresponds to the above functional modules:

 

 

 

After downloading the client code, modify the xmpphost address in the androidpn. properties file as follows:

 

After clicking submit to send the message, you can see the received message on the Android mobile phone end as follows:

Click OK to jump to the URI.

The biggest advantage of this solution is simplicity, and two problems of c2dm are avoided. With XMPP protocol, we can further expand the Protocol to achieve better functions.

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.