Android push method comparison)

Source: Internet
Author: User
Tags android push notification

Basic knowledge of push Methods
When developing an application that requires interaction with the server, we basically need to obtain the server-side data. For example, in earthquake timely, we need to obtain the latest seismic information on the server in a timely manner. Generally, there are two methods to obtain information updated from time to time on the server. The first method is that the client uses the pull (pull) method to obtain information on the server from time to time, check whether any updated information appears. The second is that the server uses the push method. When the server has new information, it pushes the latest information to the client.
Although both the pull and push methods can obtain the server-side update information, the push is better than pull. Because the pull method is more charged for the client's network traffic, more importantly, the electricity consumption.

When developing Android and iPhone applications, Zookeeper often needs to push various notification messages from the server to the mobile client in real time, the iPhone already has a simple and perfect push notification solution, but it is relatively troublesome to implement it on the Android platform, in the past few days, we have made a preliminary research on the android push notification service.

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

Alibaba Cloud 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.

C2dm operation process diagram:

However, after some research, we found that this service has a major problem:

1) c2dm is built into the Android 2.2 System and cannot be compatible with the old 1.6 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;

Zookeeper has restrictions on the use of the above two, and I finally gave up this solution, however, I want to use another article to introduce the c2dm framework in detail and the corresponding setting methods of the client and app server, which can be used for learning and reference.

Just because c2dm cannot meet our requirements, we need to implement the communication protocol between the Android mobile client and the app server on our own, ensure that the Android device receives the message in time when the app server wants to send a message to the specified Android device. Below I will introduce several common solutions:

1) Round Robin (pull): applications should be connected to the server in stages and query whether new messages arrive. You must implement communication with the server, such as Message Queuing. In addition, you also need to consider the polling frequency. If it is too slow, it may lead to delay of some messages. If it is too fast, it will consume a lot of network bandwidth and battery.

2) SMS (push): On the Android platform, you can intercept SMS messages and parse the message content to understand the server's intention. This is a good idea. I have seen applications that use this solution. The advantage of this solution is that it can achieve full real-time operations. However, the problem is that the cost of this solution is relatively high. It is difficult for you to find a free short message sending gateway.

3) persistent connection (push): this solution can solve the performance problem caused by polling, but it will still consume the cell phone's battery. Apple's push service works very well because each mobile phone only maintains a connection with the server. In fact, c2dm works like this. However, this solution also has shortcomings, that is, it is difficult for us to implement a reliable service on the mobile phone. The Android operating system allows you to kill system services with low memory, so your notification service may be killed by the operating system.

Zookeeper's first two schemes have obvious shortcomings, and the third scheme also has shortcomings. However, we can make up for them through good design so that the scheme can work effectively. After all, we need to know that Gmail, Gtalk, and googlevoice can all be updated in real time.

Android push using mqtt Protocol
Mqtt is a lightweight message publishing/subscription protocol. It is an ideal solution for message pushing servers based on mobile clients.

Wmqtt. jar is the implementation of the mqtt protocol provided by IBM. You can download it from the following website. You can add the jar package to your android application.

Really small Message Broker (rsmb), which is a simple mqtt proxy, also provided by IBM. Port 1883 is opened by default. In an application, it is responsible for receiving messages from the server and forwarding them to the specified mobile device.

Android push using XMPP protocol
Zookeeper is the solution I used in the project. In fact, Google's official c2dm server uses the XMPP protocol for encapsulation at the underlying layer.
  XMPP (Extensible communication and representation protocols) is a protocol based on 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.

Zookeeper androidpn is an open-source Java Android push notification implementation based on XMPP protocol. It contains the complete client and server. After source code research, I found that the server is basically implemented based on another open source project openfire, but it is depressing that the androidpn documentation is written in Korean, therefore, the entire research process is basically to read the source code.

The Zookeeper 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 uses this connection for user registration and login authentication. It also uses this connection to receive notifications sent by the server.

The Zookeeper 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 server architecture is as follows:

The top layer of zookeeper consists of 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 biggest advantage of the zookeeper solution is simplicity. We do not need to rely on the operating system version like c2dm, nor worry about the unavailability of Google servers one day. With XMPP protocol, we can further expand the Protocol to achieve more comprehensive functions.

Zookeeper uses this solution. Currently, we can only send text messages, but it is generally enough for pushing, because we cannot expect to get all the data through pushing. Generally, push only tells the Mobile Server that some changes have taken place. When the client receives the notification, it should take the initiative to obtain the latest data from the server. This is the complete implementation of the push service.

Summary
Zookeeper now uses the XMPP protocol for pushing more slowly, mainly because it is relatively simple. I will post some related androidpn server content in my blog post. I plan to upgrade one of my softwares, from the previous pull (pull) mode to the push mode. I believe this will reduce the pressure on the server, the score is updated and added in a timely manner.

 

From: http://www.r-base.net/archives/778

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.