Android Message Push implementation

Source: Internet
Author: User
Tags php script android push notification

When developing Android and iphone applications, we often need to instantly push notifications from the server to the mobile phone client, with a simpler and perfect push notification solution on the iphone, but the Android platform is relatively cumbersome to implement. A few days of recent use of the Android push notification service for a preliminary study.

On the Android phone platform, Google provides C2DM (cloudto Device Messaging) services, at first I was ready to use this service to implement the push function on my phone.

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.

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;

With the above two restrictions on use, which led me to finally give up the plan, but I would like to use another article to describe in detail the framework of the C2DM and the corresponding settings of the client and app server, can be used as a learning and reference.

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. Let me show you a few common scenarios:

1) Polling: 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: On the Android platform, you can understand the server's intentions by intercepting SMS messages and parsing the message contents. 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 this solution is relatively high, you can hardly find a free short message to send the gateway, about the implementation of this scheme, you may refer to the following link: https://labs.ericsson.com/apis/mobile-java-push/.

3) Persistent Connection: This solution can solve the performance problems caused by polling, but it will still consume the cell phone's battery. Apple's push service works well because each cell phone only maintains a connection to the server, and in fact C2DM works. However, there is a shortage of solutions, that is, it is difficult for us to achieve a reliable service on mobile phones. The Android operating system allows you to kill system services in low memory situations, so your notification service is likely to be killed by the operating system.

There are significant deficiencies in the first two programmes, and the third one is inadequate, 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.

? Using the MQTT protocol for Android push

MQTT is a lightweight message publishing/subscription protocol that is the ideal solution for implementing a messaging push server based on a mobile client.

We can download the instance code of the project from here, and we can find a server-side implementation with PHP writing.

The schema is as follows:

Wmqtt.jar is an implementation of the MQTT protocol provided by IBM. You can download it from the site below. You can add the jar package to your own Android app.

Really Small Message Broker (RSMB), he is a simple mqtt agent, also provided by IBM. 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. You can download it from this.

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

Instance code:

You can download instance apps from GitHub. After running the app, Access http://tokudu.com/demo/android-push/through your mobile browser, enter the device ID in the first input box, enter the message content you want to send in the second input box, press the Send Push message button, You should be able to see the notifications on your phone. You can also download the Android-push source code from this github address, which contains the send_mqtt.php script.

? Using the XMPP protocol for Android push

This is the scenario that I used in the project. 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.

ANDROIDPN is a Java open source Android push notification implementation based on the XMPP protocol. 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. It is implemented 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.

Server-side interface as follows, respectively, corresponding to the above several functional modules:

After sending, we can see the received message on the phone:

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.


Android Message Push implementation

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.