Related Knowledge and solutions pushed by the server to Android, android knowledge

Source: Internet
Author: User
Tags android push notification

Related Knowledge and solutions pushed by the server to Android, android knowledge

Basic knowledge and related solutions for implementing the push method in Android: The application scenarios of the push function in mobile phone development are getting better and better, when the news client on our mobile phone is not j, new messages are pushed to facilitate reading of the latest news. This push function is good, but we often see a lot of spam information pushed, which makes us bored. We can't say much about this, after all, many sellers need to advertise. This article is to discuss some solutions for implementing the push function in Android, and hope to play a leading role. Pai_^

 

1. Basic push method knowledge:

In the mobile Internet era, if a mobile phone needs to notify the user of an event, a window pops up to tell the user what is happening. It may be a phone call prompt, calendar reminder, or a new MMS Message. The push function was first used in Email to prompt us for new information. Due to the development of the times and the upsurge of mobile Internet, the push function is more popular, and it is no longer just used for pushing emails, but more used in our apps.

 

When developing an application that needs to interact with the server, we basically need to obtain the server data. For example, Earthquake Emergency Service needs to obtain the latest earthquake information on the server in a timely manner. There are two methods to obtain the information updated from time to time on the server: the first method is to use the Pull (Pull) method on the client, that is, to obtain the 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. In this way, the client can automatically receive messages. Bytes

 

Although both the Pull and Push methods can obtain the server-side update information, the Push method is superior to the Pull method. Because the Pull method is more expensive for the client's network traffic, and more importantly, the power consumption, we also need our programs to constantly monitor the changes on the server. Zookeeper

 

When developing Android and iPhone applications, we often need to push various notification messages to the mobile client in real time from the server. We just need to pull down the Android or IPhone Notification bar and expand the Notification Panel to view a variety of Notification messages. At present, the IOS platform already has a relatively simple and perfect push notification solution. I will introduce the solution in detail in the IPhone in the future, but it is relatively troublesome to implement it on the Android platform.

We have used a few days to conduct a preliminary study on the Android push notification service and hope to discuss it with you.


2. Implementation Principles of several common solutions:

1) Round Robin (Pull): the application should be connected to the server in stages and check whether new messages arrive. You must implement communication with the server by yourself, for example, 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 intentions and obtain the displayed content for processing. This is a good idea. I have seen applications that adopt this solution. The advantage of this solution is that it can achieve full real-time operations. But the problem is that the cost of this solution is relatively high, and we need to pay the corresponding fees to mobile companies. Currently, it is difficult to find a free gateway for sending short messages.

 

3) persistent connection (Push): this solution can solve the performance problem caused by polling, but it will still consume the cell phone's battery. The push service on the IOS platform works very well because each mobile phone only maintains a connection with the server. In fact, C2DM works like this. However, as mentioned earlier, this solution has many shortcomings, that is, it is difficult for us to implement a reliable service on our mobile phone. At present, it cannot be compared with the push function of the IOS platform.

 

The Android operating system allows the system service to be killed in the case of low memory, so our push notification service may be killed by the operating system. The poll (Pull) method and the SMS (Push) method also have obvious shortcomings. As for the persistent connection (Push) solution, there are also deficiencies, but we can make up for it through good design, so that this solution can work effectively. After all, we need to know that GMail, GTalk, and GoogleVoice can all be updated in real time.


3. solution 1: C2DM cloud push function.

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 my mobile phone, and bring it into your project. Bytes

 

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. For detailed usage of C2DM, you can refer to relevant materials. Here, let's take a look at the general solution.

 

The following is an example of the C2DM operation process:


 

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;


 

With these constraints, 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 as a learning resource for reference. As C2DM cannot meet our requirements, we need to implement the communication protocol between the Android mobile client and the App Server on our own to ensure that when the App Server wants to send messages to a specified Android device, android devices can receive messages in a timely manner.

 

4. solution 2: implement the Android push function using the MQTT protocol.

Using MQTT to implement Android push is also a solution. 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. We can download the instance code for this project from here (https://github.com/tokudu/AndroidPushNotificationsDemo) and find a server-side implementation (https://github.com/tokudu/PhpMQTTClient) written in PHP ).

 

Shows the architecture:


 

Wmqtt. jar is the implementation of the MQTT protocol provided by IBM. We can download from the following site (http://www-01.ibm.com/support/docview.wss? Rs = 171 & uid = swg24006006) it. We can add this jar package to our Android Application.

 

5. solution 3: implement the push function of RSMB.

Really Small Message Broker (RSMB) is a simple MQTT proxy, which is also provided by IBM at http://www.alphaworks.ibm.com/tech/rsmb. 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.

 

SAM is a PHP library written for MQTT. We can download it from the address http://pecl.php.net/package/sam/download/0.2.0.

Send_mqtt.php is a PHP script that receives messages through POST and sends messages to RSMB through SAM.

 

6. solution 4: XMPP protocol for Android push.

This is the solution I want to use in the project, because it is currently open-source and can be implemented for its simple push function. We can modify the source code to adapt to our application.

In fact, Google's official C2DM server uses the XMPP protocol for encapsulation at 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. About XMPP protocol I have introduced in the previous blog, you can refer to the next article: http://www.cnblogs.com/hanyonglu/archive/2012/03/04/2378956.html

 

Androidpn is an open-source java Android push notification implementation based on XMPP protocol. I will introduce androidpn in a later blog. 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.

 

This is androidpn project home page: http://sourceforge.net/projects/androidpn/

 

Shows the androidpn implementation intent:


 

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 uses this connection for user registration and login authentication. It also uses this connection to receive 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 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 biggest advantage of this 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. Using this scheme, we can only send text messages at present, 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. The XMPP agreement is relatively simple and worthy of further research.

 

However, after a period of testing, I found that androidpn also has some shortcomings:

1. For example, if it takes too long, the push information will no longer be received.

2. Performance is not stable enough.

3. If messages are pushed from the server, they are no longer managed, regardless of whether the messages have successfully arrived on the client's mobile phone.

 

In short, androidpn also has many disadvantages. If we want to use androidpn, we still need to do a lot of work.

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

 

7. Solution 5: Use a third-party platform.

Third-party platforms are commercially available and free of charge. We can use them as needed. For third-party platforms in China, I feel that Aurora push is a good one. Aurora push is currently free of charge and can be used directly. For details, refer to its homepage: http://www.jpush.cn/index.jsp.

 

About MQTT solutions, the domestic recent emergence of mqtt-based third-party solutions cloud Ba (http://yunba.io), it is understood that the original Aurora push CTO founded, interested friends can study.


I have also seen several third-party platforms abroad: http://www.push-icationication.org /. If you are interested, you can check related information. If you use a third-party platform, you need to use someone else's server. You know this.

 

8. Solution 6: Build a push platform by yourself.

This is not an easy task. Of course, you can adopt appropriate solutions based on your own needs.

 


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.