Android Implementation Push mode solution

Source: Internet
Author: User
Tags android push notification

Android Implementation Push mode solution

This article describes the basics and related solutions for pushing in Android. Push function in the mobile phone development of the application of the scene is the more come up, do not say anything else, on our mobile phone news client on the time when the push came over the new message, very convenient to read the latest news information. This push function is the good side, but also often see a lot of spam messages pushed over, which makes us bored, we can not say anything about this, after all, many businesses to advertise. This article is to explore the next Android to implement the push function of some solutions, but also hope to play a role. ^_^

1. Basic knowledge of Push mode:

In the mobile Internet era before the mobile phone, if something happens to notify the user, there will be a window pops up, will tell the user what is happening. May be a missed call, a calendar reminder, or a new MMS message. The push feature was first used in email to prompt us for new information. Due to the development of the Times and the upsurge of the mobile internet, the push function is more popular, it is no longer just used in the push mail, more in our app.

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. There are generally two ways to get information about updates on the server that are not timed: the first is the way the client uses pull, which is to get information from the server at a time and see if there is any updated information. The second is the way the server uses push (push), when new information is available on the server side, push the latest information to the client. This allows the client to automatically receive the message.

While pull and push two ways to get server-side update information, it is obvious that push is more advantageous than pulling mode. Because the pull way more than the client's network traffic, the main is the charge, but also need our program constantly to monitor the changes in the server.

When developing Android and iphone applications, we often need to instantly push various notification messages from the server to the mobile client. We only need to pull down at the notification bar of Android or iphone to expand the notification Panel to focus on a variety of notification messages. There's a simple and perfect push notification solution available on the iOS platform, and I'll cover the iphone solution in detail later on, but the Android platform is relatively cumbersome to implement.

The recent use of a few days of the Android push notification service to carry out a preliminary study, but also hope to discuss with you.


2. Several common solution implementation principles:

1) Polling method: 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) mode: On the Android platform, you can intercept the SMS message and parse the message content to understand the server's intentions and get its display content for processing. 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 the solution is relatively high, and we need to pay the cost to the mobile company. It is difficult to find a free short message sending gateway to implement this scenario.

3) Persistent connection (Push) mode: This solution can solve the performance problems caused by polling, but still consumes the battery of the phone. The push service for the iOS platform works well because each phone only maintains a connection to the server, and in fact C2DM works. But just now, there are a lot of shortcomings in this solution, that is, it is difficult for us to achieve a reliable service on the mobile phone, and currently can not be compared with the push function of the iOS platform.

The Android operating system allows system services to be killed in low-memory situations, so our push notification service is likely to be dropped by the operating system. There are also significant deficiencies in the two scenarios of polling (pull) and SMS (Push) methods. There are also shortcomings with the persistent connection (Push) scheme, but we can make up for it with good design, so that the solution can work effectively. After all, we need to know that both Gmail,gtalk and Googlevoice can be updated in real time.


3. First solution: C2DM Cloud push feature.

On the Android phone platform, Google provides C2DM (cloudto Device Messaging) services, and at first I was ready to use the service to implement the push feature on my phone and bring it into my project.

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, you can go to the relevant information, here first let us understand the approximate scenario.

The following is an example diagram of the C2DM operation process:


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;

3) Unlike on the iphone, they integrate the hardware system into one piece. So for our developers, if we want to use C2DM push in our application, because of the different hardware vendors platform, such as Motorola, Huawei, ZTE do a mobile phone, they may be Google's services to remove, especially as in the domestic many such, Get rid of this native service from Google. Buying something like a cottage or a Huawei-made machine, Google's services may not be available. And things like those out there might be built in.

With the above constraints, I finally gave up the plan, but I would like to use another article to describe in detail the framework of C2DM and the corresponding settings of the client and app server, can be used as a learning resource to let us have a reference material. 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.

4. The second solution: the MQTT protocol implements the Android push feature.

The use of the MQTT protocol for Android Push is also a solution. MQTT is a lightweight message publishing/subscription protocol that is the ideal solution for implementing a messaging push server based on a mobile client.

Wmqtt.jar is an implementation of the MQTT protocol provided by IBM. We can download the instance code of the project from here (Https:// and find a server-side implementation that is written in PHP (https://

The schema looks like this:


Wmqtt.jar is an implementation of the MQTT protocol provided by IBM. We can download it from the following site ( We can add the jar package to our Android app.

5. The third solution: RSMB implementation of push functionality.

Really Small Message Broker (RSMB), he is a simple mqtt agent, also provided by IBM, whose viewing address is: HTTP://WWW.ALPHAWORKS.IBM.COM/TECH/RSMB. 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 written for Mqtt. We can download it from this address.

send_mqtt.php is a PHP script that receives messages via post and sends messages to RSMB via Sam.

6. Fourth solution: The XMPP protocol enables Android push functionality.

This is the scenario I want to use in my project because it is open source and can be implemented for its simple push functionality. We can modify its source code to fit our application.

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 the XMPP protocol I have described in the previous blog post, you can refer to the following article:

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.

This is ANDROIDPN's project. Home:

The ANDROIDPN 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. The XMPP protocol is relatively simple and deserves further study.

But after a period of testing, I found that there are some deficiencies in ANDROIDPN:

1. For example, when the time is too long, you can no longer receive push messages.

2. The performance is not stable enough.

3. If the message is pushed out of the server, it is no longer managed, regardless of whether the message successfully arrives on the client phone.

Wait, in short, ANDROIDPN also has a lot of shortcomings. If we want to use ANDROIDPN, we still need to do a lot of work.

As for the detailed use process, we will introduce it to you in the next blog post.

7. Fifth solution: Use of third-party platforms.

Third-party platforms are commercially available and free of charge, and we can use them according to the implementation situation. On the domestic third-party platform, I feel that the more good is a push push. There are a lot of big partners are using a push, now also launched a precision push Push 2.0. We can use it directly. For details, you can view its homepage:, which is no longer described in detail here.

8. Sixth solution: Build a push platform yourself.

This is not an easy job, of course, according to their own needs to adopt the appropriate plan.

Okay, here's the basics and solutions for pushing in Android.

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.