Android push Solution Analysis (MQTT/XMPP/GCM)

Source: Internet
Author: User

Solution 1. Use the GCM Service (Google Cloud Messaging)
Introduction: Alibaba Cloud Message Service (G2DM) launched by Google is the second generation.
Advantages: the services provided by Google are native and simple, and there is no need to implement or deploy the server.
Disadvantage: the Android version is limited (must be later than Version 2.2). This service is not stable enough in China and users need to bind Google accounts, because Google is limited.

Solution 2: Use XMPP protocol (Openfire + Spark + Smack)
Introduction: XML-based communication protocols, formerly Jabber, have been standardized by the IETF International Standardization Organization.
Advantages: the Protocol is mature, powerful, and highly scalable. Currently, it is mainly used in many chat systems and has an open-source Java version Development Instance androidpn.
Disadvantages: the Protocol is complex, redundant (based on XML), paybytraffic, power consumption, and high hardware deployment costs.

Solution 3: Use the MQTT Protocol
Introduction: a lightweight and proxy-based message transmission protocol in the publish/subscribe mode.
Advantages: the Protocol is simple, compact, highly scalable, traffic-saving, and power-saving. It has been applied to the enterprise field (reference:
The server component rsmb of C ++ already exists.
Disadvantages: not mature enough, complicated implementation, the server component rsmb is not open-source, and the hardware deployment cost is high.

Solution 4: HTTP round robin
Brief: regularly obtain the latest message from the HTTP Service API.
Advantages: easy to implement, strong controllability, and low hardware deployment costs.
Disadvantage: poor real-time performance.

We recommend that you use the MQTT protocol to study and compare the advantages and disadvantages of each solution. The main reason is that MQTT is the fastest and most traffic-saving (the Fixed Header length is only 2 bytes ), it is easy to expand and is suitable for secondary development. Next, we will analyze the principles and methods of using the MQTT solution for Android messages, and set up our own PUSH Service.

1. Analysis of push principles

 

In fact, the principles of other push systems (including GCM and XMPP) are similar.

2. Prepare the push Server

A> download and decompress the rsmb installation package
B> enter the corresponding directory. For example, a 32-bit Linux system should enter the linux_ia32 directory.
C> edit the configuration file broker_1883.cfg. The configuration is as follows:

[Html] view plain copy
 
Port 1883
Max_inflight_messages 10
Max_queued_messages 1000
D> Run./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. 2007,201 0 All Rights Reserved
20120823 110454.039 CWNAN9995I US Government Users Restricted Rights-Use, duplication or disclosure restricted by gsa adp Schedule Contract with IBM Corp.
20120823 110454.039 CWNAN0049I Configuration file name is broker_1883.cfg
20120823 110454.040 CWNAN0053I Version 1.2.0, Aug 18 2010 17:03:35
20120823 110454.040 CWNAN0054I Features completed ded: bridge
20120823 110454.040 CWNAN9993I Author: Ian Craggs (icraggs@uk.ibm.com)
20120823 110454.040 CWNAN0014I MQTT protocol starting, listening on port 1883
......
In this way, the PUSH Service server is ready to listen to port 1883.

3. Prepare the push client

A> download and decompress the AndroidPushNotificationsDemo Project
B> import the project to Eclipse (File-> Export-> Existing Projects into Workspace)
C> modify the MQTT_HOST constant in PushService. java to the IP address of the push server.
D> Start the Android simulator and install the project.

Note: The following errors may occur in the new 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 35 'lcom/ibm/mqtt/MqttSimpleCallback ;'
08-23 02:28:44. 194: W/dalvikvm (282): Link of class 'lcom/tokudu/demo/PushService $ MQTTConnection; 'failed
02:28:44-23. 194: E/dalvikvm (282): cocould not find class 'com. tokudu. demo. pushService $ MQTTConnection ', referenced from method com. tokudu. demo. pushService. connect
02:28:44-23. 194: W/dalvikvm (282): VFY: unable to resolve new-instance 42 (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. sharemclick (View. java: 2408)
08-23 02:28:44. 404: E/AndroidRuntime (282): at android. view. View $ sharemclick. 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 during the release. The solution is as follows:
1> Create the libs directory under the project root directory and move the wmqtt. jar package to this directory.
2> re-configure the target Java Build Path (right-click the Properties option in the menu ).
3> repackage and release.

The running effect is as follows:

 

Click the Start Push Service button to enable the Push Service. The following prompt is displayed in the rsmb Service Log:
20120823 113742.297 CWNAN0033I Connection attempt to listener 1883 received ed from client tokudu/9774d56d682e549c on address 192.168.28.39: 3345
The "9774d56d682e549c" is the corresponding client ID.

4. Sending service preparation

A> download &decompress the PHP version's sending service code send_mqtt.zip
B> modify the IP address and port number of the push server in etc/config. php, that is, the MQTT_SERVER_HOST and MQTT_SERVER_POST constants.
C> open the corresponding URL address and you will see the sending service interface, which actually pushes messages to the corresponding push client.

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.