Android Apps news push Perfect program sharing

Source: Internet
Author: User
Tags auth php script port number tomcat android push notification advantage

1. Message Push base

Message push is a new technology that can reduce information overload by regularly transmitting information that users need on the Internet. Push technology reduces the time it uses to search on the Web by automatically sending information to users. It searches for and filters information based on the interest of the user, and pushes it to the user on a regular basis to help users find valuable information efficiently

When we develop mobile applications that need to interact with the server, we basically need to interact with the server, including uploading data to the server and getting the data from the server.

In general, the client and server communication between the client is active, but there is a problem is that once the server data updates or the server to send a notification to the client can only wait for the client connection time to achieve. This way the message loses its real-time nature.

How to enable the client to receive real-time server messages and notifications, in general there are two ways, the first is the client use Pull (pull) the way, is a time to go to the server to obtain information to see if there are updated information appears. The second is the way the server uses push (push) to push the latest information to the client when there is new information on the server side. This allows the client to receive the message automatically.

Although the pull and push two ways can achieve the ability to obtain server-side update information, it is obvious that the push method is superior to the pull mode. Because the pull way more cost client network traffic, more important is the charge, still need our program to continuously monitor the change of service end.

2. Several common solution realization principle

1 polling (Pull) mode: The client sends a query message to the server periodically, and synchronizes the message as soon as the server changes.

2 SMS (Push): Understand the commands of the server by intercepting SMS messages and parsing the content of the message, but this is an affordable way for the average user.

3 Persistent Connection (Push): The client and the server to establish a long-term connection, so that the timely implementation of the message line and real-time.

3. Message Push Solution Overview

A, C2DM Cloud Push Program

On the Android phone platform, Google offers C2DM (cloudto Device messaging) services. The Android Cloud to Device messaging (C2DM) is a service that helps developers send data from the server to the Android application. The service provides a simple, lightweight mechanism that allows the server to notify mobile applications to communicate directly with the server to get application updates and user data from the server.

The main problem with the program is that C2DM relies on Google's official C2DM servers, which are often unavailable due to the domestic network environment.

B, MQTT protocol to achieve Android push

Using the MQTT protocol to implement the Android push function is also a solution. MQTT is a lightweight messaging/subscription protocol that is the ideal solution for implementing a message Push server based on a mobile client.

Wmqtt.jar is the implementation of the MQTT protocol provided by IBM. We can download the project's instance code from here (Https://github.com/tokudu/AndroidPushNotificationsDemo), and we can find a server-side implementation that uses PHP writing (https:// github.com/tokudu/phpmqttclient).

C, RSMB implementation push function

Really Small message broker (RSMB) is a simple MQTT proxy, also provided by IBM, whose view address is: HTTP://WWW.ALPHAWORKS.IBM.COM/TECH/RSMB. The default is to open port 1883, 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 http://pecl.php.net/package/sam/download/0.2.0 address.

D, XMPP protocol to achieve Android push

Google's official C2DM server is also packaged using the XMPP protocol. XMPP (Extensible Communication and Presentation Protocol) is based on Extensible Markup Language (XML) protocol, which is used for instant messaging (IM) and online probing. The agreement could eventually allow Internet users to send instant messages to anyone else on the Internet.

ANDROIDPN is a Java open source Android push notification implementation based on the XMPP protocol. It contains the full client and server side. But there are also some deficiencies:

1 For example, when the time is too long, it can no longer receive the information pushed.

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 was successfully reached on the client phone.

If we want to use ANDROIDPN, then we need to do a lot of work, need to understand the XMPP protocol, understand ANDROIDPN implementation mechanism, need to debug internal bugs.

E, using a third party platform

At present, some of the domestic and foreign push platform to use, but it involves fees, confidentiality issues, quality of service issues, expansion issues, and so on, we have to be discouraged.

4, Message push the perfect solution

Combined with the above discussion, in the establishment of Android message push is a lot of scenarios, but each one has its advantages and disadvantages. However, it is the best policy to build a push platform. Because you have, he has less than himself.

On the construction of its own push platform recommended the use of the "9th-liter Android Message push component" (http://www.bjjrs.net/product/13629681868537.html). The group can be used not only to use, but also to provide the source code to expand to achieve their own special needs.

A, Push principle

9th + Android Message push components implement Android push based on XMPP protocol. XMPP (Extensible Communication and Presentation Protocol) is based on Extensible Markup Language (XML) protocol, which is used for instant messaging (IM) and online probing. The agreement could eventually allow Internet users to send instant messages to anyone else on the Internet.

9th the Android Message Push component implementation principle see figure below:


Figure 1-Message push schematic diagram

9th + Android Message push components consist of server parts and client parts. Each part is composed of a XMPP protocol component and an external interface component. The XMPP protocol component is responsible for connection management and messaging between the server and the Android client, and the external interface component is responsible for receiving the application system, client application commands, and sending the received notification message to the application system.

The 9th-liter Android Messaging component provides Tomcat based server applications and Android development jar packs. The Tomcat-based server application is deployed directly on Tomcat, and the Android development jar package is introduced into the Android project.

B Integration Mode

1), Server deployment

9th + Android Messaging component Tomcat's server application is deployed directly in Tomcat and the port number is set arbitrarily.

2), client jar package Reference

Create the Libs directory in the Android project, and then copy the supplied Android development jar package to the directory. See figure below:



Figure 2-jar Package Introduction diagram

3), Android project Androidmanifest.xml file modification

Add the following permissions to the file:

<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>

<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name= "Android.permission.INTERNET"/>

<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>

<uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE"/>

<uses-permission android:name= "Android.permission.VIBRATE"/>

Register the service in this file:

<service android:enabled= "true"

Android:name= "Com.bjjrs.server.NotificationService"

Android:label= "Notificationservice" >

<intent-filter>

<action android:name= "Com.bjjrs.server.NotificationService"/>

</intent-filter>

</service>

Up to this point, 9th, the Android Messaging component integration work is complete.

C, interface mode

1, server-side interface using HTTP protocol access, using HTTP protocol from the server to obtain a variety of information to achieve the push of notification messages.

Queries for various user messages can be implemented using the following methods and parameters:

http://localhost:8080/user.do?action=getalluser&isonline=&userid=&usertype=&deptid=& Deptname=&realname=

You can implement various messages by using the following methods:

Http://localhost:8080/notification.do?action=pushNoti&userNames=&title=&content=

2), the Android Client interface uses broadcast mechanism.

Message receipt: When the XMPP protocol component receives a push message, it broadcasts the message in a certain format notifying the client that other applications receive and process the message.

Message sending: When a client application needs to send an instant message to a server or other client, it simply broadcasts the message in a certain format, and the XMPP component automatically receives the message and sends it to the other client specified.

D. Advantages and characteristics

1, system integration is simple, no need for complex settings.

2, the Android client application and the 9th-liter Android message push component are completely separated, invoke each other through the interface, realize the module application optimization.

3, the client communication mechanism uses the broadcast way, to the client application to bring the great flexibility and the expansibility, may handle receives the push message freely.

4), 9th-liter Android message push components have message storage on the server side, message resend, message routing and other functions, in the client part of the connection with the disconnection, receipt of confirmation, read confirmation, message delivery, command execution and other functions to ensure that the message can be pushed to the client, but also to ensure that the client can receive, Read the message.

E, Application scope

9th-liter Android message push components can be used in the following scenarios:

1), for message push. Such as: Notice issued, emergency command and so on.

2), the user timely message interaction. such as online chat, work interaction and so on.

3), for remote control. such as controlling the status of remote clients, data escalation, and so on.


Android message push mechanism


1. Basic knowledge of Push mode:

When we develop an application that needs to interact with the server, we basically need to get the server-side data, such as "Earthquake emergency pass" to get the latest seismic information on the server in time. To get information that is not regularly updated on the server generally there are two methods, the first is the client use Pull (pull), the way to the server at intervals to obtain information to see if there is updated information. The second is the way the server uses push (push) to push the latest information to the client when there is new information on the server side.

Although pull and push two ways can achieve the ability to obtain server-side update information, it is obvious that the push is better than pull. Because pull way more cost the network traffic of the client, more main is charge electricity.

In the development of Android and iphone applications, we often need to from the server to the mobile client to immediately push all kinds of notification messages, the iphone has a relatively simple and perfect push notification solution, I will later in detail on the iphone solution, But the Android platform is relatively cumbersome to implement, and has recently taken a few days to make a preliminary study of Android's push notification service. On the Android phone platform, Google provides C2DM (cloudto Device Messaging) service, and at first I was ready to use the service to implement the push function on my phone.

The Android Cloud to Device messaging (C2DM) is a service that helps developers send data from the server to the Android application. The service provides a simple, lightweight mechanism that allows the server to notify mobile applications to communicate directly with the server to get application updates and user data from the server. The C2DM service is responsible for handling transactions such as Message Queuing and distributing these messages to applications running on target devices. About C2DM specific use of the process, I will later blog in detail, here we first understand the general scheme.

C2DM Operation Process Diagram:


But after some research has found that the service has a big problem:

1 C2DM is placed on the Android 2.2 system and cannot be compatible with old 1.6 to 2.1 systems;

2 C2DM need to rely on Google to provide the official C2DM server, due to the domestic network environment, this service is often unavailable, if you want to use, our app server must also be abroad, this is not possible for every developer can achieve; With these two restrictions on use, I finally gave up the solution, but I want to use another article to detail the framework of the C2DM and the corresponding settings for the client and app server, can be used as learning and reference. While C2DM is unable to meet our requirements, we need to implement a communication protocol between the Android phone client and the app server to ensure that the Android device is received in a timely manner when app Server wants to send a message to the specified Android device.

2. Several common solutions

1 Polling (Pull): The application should be phased in with the server to connect and query whether there are new messages arrived, you have to implement the communication between the server, such as Message Queuing. And you also have to consider the frequency of polling, if too slow may cause some message delay, if too fast, it will consume a lot of network bandwidth and battery.

2 SMS (Push): On the Android platform, you can understand the intent of the server by intercepting SMS messages and parsing message content. This is a good idea and I have seen applications that use this program. The advantage of this scheme is that it enables full real-time operation. But the problem is that the cost of this program is relatively high, you can hardly find a free SMS gateway, about the implementation of this scheme.

3 Persistent Connection (Push): This solution can solve the performance problem caused by polling, but it still consumes the cell phone battery. Apple's push service works well because each handset only maintains a connection to the server, and in fact C2DM works. But there is also a shortage of solutions that make it difficult to achieve a reliable service on a mobile phone.

The Android operating system allows you to kill system services in low memory conditions, so your notification service is likely to be killed by the operating system. There are obvious deficiencies in the first two programmes, and there is a shortfall in the third, but we can make up for it through good design so that the programme can work effectively. After all, we need to know that both Gmail,gtalk and Googlevoice can be updated in real time.

3. MQTT protocol to achieve Android push

Using the MQTT protocol to implement the android push Mqtt is a lightweight message publishing/subscription protocol, which is the ideal solution for implementing a message Push server based on mobile clients. Wmqtt.jar is the implementation of the MQTT protocol provided by IBM. We can download the project's instance code from here, and we can find a server-side implementation that uses PHP writing.

The schema looks like this:


Wmqtt.jar is the implementation of the MQTT protocol provided by IBM. We can download it from the following site. You can add this jar package to your own Android application.

4.RSMB implementation push:

Really Small message broker (RSMB), a simple MQTT agent, is also provided by IBM. The default is to open port 1883, 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.

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

Really Small message broker (RSMB), a simple MQTT agent, is also provided by IBM. The default is to open port 1883, which is responsible for receiving messages from the server and forwarding them to the specified mobile device.

5. XMPP protocol to achieve Android push

This is the program I used in the project. In fact, Google's official C2DM server is also the bottom of the XMPP protocol package. XMPP (Extensible Communication and Presentation Protocol) is based on Extensible Markup Language (XML) protocol, which is used for instant messaging (IM) and online probing. The agreement could eventually allow Internet users to send instant messages to anyone else on the Internet.

ANDROIDPN is a Java open source Android push notification implementation based on the XMPP protocol, and I will introduce ANDROIDPN in detail later posting. It contains the full client and server side. After the source code research I found that the server is basically in another open source engineering openfire on the basis of the implementation, but the more depressing is that the ANDROIDPN document is written by Korean, so the whole research process is basically read source.

The implementation intent is shown in the following illustration:


ANDROIDPN client needs to use a Java based open source XMPP protocol package Asmack, which is also based on the openfire of another open source project smack, but we do not need to compile, You can use the 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 through the connection for user registration and login authentication, as well as a notification sent by the receiving server through this connection.

The ANDROIDPN server side is also implemented in the Java language, based on OpenFire Open source Engineering, but its Web Part uses the spring framework, which is different from OpenFire. The ANDROIDPN server contains two parts, one is listening to the XMPP service on the 5222 port, is responsible for communicating with the client Xmppconnection class, the function is the user registers and the authentication, and sends the push notification message. The other part is a Web server that takes on a lightweight HTTP server and is responsible for receiving Web requests from users. The server schema is as follows:


The top layer contains four components, namely Sessionmanager,auth Manager,presencemanager and notification Manager. SessionManager is responsible for managing the session between the client and the server, Auth Manager is responsible for client user authentication management, and Presence Manager is responsible for managing the client user's logon status. Notificationmanager is responsible for implementing the server's push-message function to the client.

The biggest advantage of this solution is simplicity, we don't have to rely on the OS version like C2DM, nor do we worry about a day when Google server is unavailable. Using the XMPP protocol, we can further extend the protocol and realize more perfect function. With this option, we can now only send text messages, but for the push is generally enough, because we can not rely on the push to get all the data, in general, the use of push is only to tell the mobile phone server has changed some, when the client received notification, should be active to the server to obtain the latest data, This is the full implementation of the push service.


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.