Go Android Push Scenario analysis (MQTT/XMPP/GCM)

Source: Internet
Author: User

Transferred from: http://m.oschina.net/blog/82059

The main purpose of this article is to analyze and contrast the most mainstream message push schemes on the Android platform, objectively reflect the advantages and disadvantages of these push schemes, and help us choose the most suitable implementation plan.

Scenario 1,Using GCM Services (Google Cloud Messaging)
Introduction: Google launched the cloud messaging service, the second generation of G2DM.
Pros: Google offers services, native, and simple, without the need to implement and deploy the server.
Cons: Android version limit (must be greater than 2.2 version), the service is not stable in the country, users need to bind Google account, limited by Google.

Scenario 2,Using the XMPP protocol (OpenFire + Spark + Smack)
Introduction: The communication protocol based on XML protocol, formerly known as Jabber, has been standardized by the IETF International Organization for Standardization.
Advantages: The protocol is mature, strong, extensible, is mainly used in many chat systems, and has open source Java version of the development example ANDROIDPN.
Disadvantage: The protocol is more complex, redundant (based on XML), charge flow, charge power, and the cost of deploying hardware is high.

Scenario 3,Using the MQTT protocol (for more information see: http://mqtt.org/)
Summary: A lightweight, agent-based "Publish/subscribe" mode message Transfer Protocol.
Advantages: The protocol is concise, small, scalable, and provincial traffic, power saving, has been applied to the Enterprise Domain (reference: http://mqtt.org/software), and has a C + + version of the service-side components RSMB.
Disadvantage: not mature enough, achieve more complex, service-side components RSMB not open source, the deployment of hardware cost is high.

Scenario 4,Using the HTTP round robin method
Summary: Get the latest news from the HTTP server-side interface (WEB service API) on a timed basis.
Advantages: Easy to implement, strong controllability, low cost of deployment hardware.
Disadvantage: Poor real-time.

The research and comparison of the advantages and disadvantages of each scheme is recommended by using the MQTT protocol scheme, mainly due to:Mqtt is the fastest and most efficient (with a fixed head length of only 2 bytes) and easily expandable for two development。 Next, we analyze the principles and methods of using the MQTT solution for Android messages and set up our own push service.

1. Push principle Analysis




In fact, the principle of other push systems, including GCM and XMPP schemes, is similar.

2, push service side preparation

a> Download & Unzip RSMB installation package (: HTTP://WWW.ALPHAWORKS.IBM.COM/TECH/RSMB)
B> into the corresponding directory, such as the 32-bit Linux system should enter the Linux_ia32 directory.
C> Edit the configuration file Broker_1883.cfg, configured as follows:

[HTML]View Plain Copy
    1. Port 1883
    2. Max_inflight_messages 10
    3. Max_queued_messages 1000

D> run the./broker broker_1883.cfg, as shown below:
20120823 110454.039 cwnan9999i really Small Message Broker
20120823 110454.039 cwnan9997i Licensed Materials-property of IBM
20120823 110454.039 cwnan9996i Copyright IBM Corp. All Rights Reserved
20120823 110454.039 cwnan9995i US Government Users Restricted rights-use, duplication or disclosure Restricted by GSA AD P Schedule contract with IBM Corp.
20120823 110454.039 cwnan0049i Configuration file name is Broker_1883.cfg
20120823 110454.040 cwnan0053i Version 1.2.0, 18 2010 17:03:35
20120823 110454.040 cwnan0054i Features Included:bridge
20120823 110454.040 cwnan9993i Author:ian craggs ([email protected])
20120823 110454.040 cwnan0014i MQTT protocol starting, listening on port 1883
... ...
This way, the service side of the push service is ready to listen on port 1883.

3. Push Client Preparation

a> Download & Unzip Androidpushnotificationsdemo project (: Https://github.com/tokudu/AndroidPushNotificationsDemo)
B> the project into eclipse (File--Export-Existing Projects into Workspace)
C> Modify the Pushservice.java in theMqtt_hostThe constant is the IP address of the push service side.
D> start the Android emulator and install the project.

Note:The following errors may be encountered in the new version of the Android SDK.
... ...
08-23 02:28:44.184:W/DALVIKVM (282): Vfy:unable to find class referenced in signature (lcom/ibm/mqtt/mqttpersistence;)
08-23 02:28:44.194:I/DALVIKVM (282): Failed resolving lcom/tokudu/demo/pushservice$mqttconnection; interface ' Lcom/ibm/mqtt/mqttsimplecallback; '
08-23 02:28:44.194:W/DALVIKVM (282): Link of Class ' lcom/tokudu/demo/pushservice$mqttconnection; ' failed
08-23 02:28:44.194:E/DALVIKVM (282): Could not find class ' com.tokudu.demo.pushservice$mqttconnection ', referenced from Method Com.tokudu.demo.PushService.connect
08-23 02:28:44.194:W/DALVIKVM (282): vfy:unable to resolve New-instance (lcom/tokudu/demo/pushservice$ mqttconnection;) in Lcom/tokudu/demo/pushservice;
... ...
08-23 02:28:44.404:e/androidruntime (282): Java.lang.VerifyError:com.tokudu.demo.PushService
08-23 02:28:44.404:e/androidruntime (282): at Com.tokudu.demo.pushactivity$1.onclick (PushActivity.java:32)
08-23 02:28:44.404:e/androidruntime (282): at Android.view.View.performClick (view.java:2408)
08-23 02:28:44.404:e/androidruntime (282): at Android.view.view$performclick.run (view.java:8816)
08-23 02:28:44.404:e/androidruntime (282): at Android.os.Handler.handleCallback (handler.java:587)
08-23 02:28:44.404:e/androidruntime (282): at Android.os.Handler.dispatchMessage (handler.java:92)
08-23 02:28:44.404:e/androidruntime (282): at Android.os.Looper.loop (looper.java:123)
08-23 02:28:44.404:e/androidruntime (282): at Android.app.ActivityThread.main (activitythread.java:4627)
08-23 02:28:44.404:e/androidruntime (282): At Java.lang.reflect.Method.invokeNative (Native Method)
08-23 02:28:44.404:e/androidruntime (282): at Java.lang.reflect.Method.invoke (method.java:521)
08-23 02:28:44.404:e/androidruntime (282): At Com.android.internal.os.zygoteinit$methodandargscaller.run ( zygoteinit.java:868)
08-23 02:28:44.404:e/androidruntime (282): at Com.android.internal.os.ZygoteInit.main (zygoteinit.java:626)
08-23 02:28:44.404:e/androidruntime (282): At Dalvik.system.NativeStart.main (Native Method)
... ...
The reason is that the Wmqtt.jar package was not added at the time of publication, the solution is as follows:
1> Create the Libs directory under the project root directory and move the Wmqtt.jar package into the directory.
2> reconfigure the project's Java Build Path (in the Properties option in the context menu).
3> re-package the release.

The results are as follows:



Click the "Start Push Service" button to open the push service. At this point we can see the following prompt in RSMB's service log:
20120823 113742.297 cwnan0033i Connection attempt to listener 1883 received from client tokudu/9774d56d682e549c on address 192.168.28.39:3345
The "9774d56d682e549c" is the corresponding client ID number.

4, send the service preparation

a> Download & Unzip the PHP version of the Send server code send_mqtt.zip (: http://download.csdn.net/detail/shagoo/4520102)
b> Modify the IP address and port number of the push service side in the etc/config.php, i.e.Mqtt_server_hostAndMqtt_server_postConstant.
c> open the corresponding URL address, you can see the interface of the sending service, actually push the message to the corresponding push client.



Next, we fill in the interface with the client ID (9774d56d682e549c) and push message (test) and click on the "Send Push Message" button, the server can push the message to the client. We see that the client can immediately receive the message just pushed, such as.



Of course, there are many deficiencies in the above scenario, for example, if the client does not keep the connection, the messages sent will be discarded. However, we can use the MQTT protocol to develop a more powerful server to replace the RSMB, but also to join the queue, caching and other functions to optimize, interested friends may wish to try. Refer to open source projectsMosquitto(http://mosquitto.org/), related content we will be in the next "installation and use of Mosquitto services" described.

Go Android Push Scenario analysis (MQTT/XMPP/GCM)

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.