Message push on mobile phone

Source: Internet
Author: User
Recently, I am looking for a message push solution on the Android mobile phone. Currently, there are several common methods:

1. Regular query: connect to the server for the latest message according to the specified interval. It is easy to implement and non-real-time. If the query time is too short, the traffic consumption is high and the power consumption is high. Here is an Ericsson Test Result:

 

2. Text message method: This method can also be used to send messages to clients in a timely manner, but everyone understands that this is very costly.

 

3. Long polling: basically the same method used by many websites (Web aliwangwang, Weibo, Renren, etc ). The client initiates a long time-out request, and the server blocks the request when there is no message (the request is never returned) until it is about to time out, and a response is returned when a message arrives. The client initiates a request immediately after receiving the response or timeout.

This is a good way to get messages to the client in a timely manner. However, considering the characteristics of the mobile Internet (the network is unstable and the device memory is small), this method cannot ensure that important messages can be pushed to the client, in addition, the anroid may kill the process that is not very active while waiting for the push message when the phone memory is small.

 

4. c2dm: Google provides the message push function, which needs to be bound to the Google account. Currently, this method is not quite feasible in China.

 

5. XMPP: Integrating asmack on the client, and using open-source XMPP server software such as ejabberd or openfire on the server is also a feasible method.

The disadvantage is that there must be registration, login, and other processes first. The effect of connection in the wireless network environment is not very good. To push important messages, you must implement the validation logic yourself.

 

6. mqtt: message transmission protocol based on the proxy publishing/subscription mode, applicable to restricted environments:

Expensive network, low bandwidth, and unreliable;

There are limited resources for running, processor, and memory on embedded devices.

Features:

Release an application in the publish/subscribe ModeProgramCoupling;

Message transmission blocked from the load content;

Use TCP/IP;

Provides "up to once", "at least once", and "only once" message transmission;

Small-scale transmission with low traffic overhead;

Use the last will and testament features to notify relevant parties about the abnormal client interruption mechanism.

(It sounds like it was designed for the mobile Internet.)

The following is a simple implementation scheme based on mqtt:

 

Server:

The IBM mqtt server rsmb can be used;

Open-source mosquitto

 

Client:

IBM wmqtt. jar is applicable to Java clients.

 

1. download and install the mosquitto server.

2. Integrate the following in the anroid client:Code:

 

Import Com. IBM. mqtt. imqttclient;
Import Com. IBM. mqtt. mqttclient;
Import Com. IBM. mqtt. mqttexception;
Import Com. IBM. mqtt. mqttpersistence;
Import Com. IBM. mqtt. mqttpersistenceexception;
Import Com. IBM. mqtt. mqttsimplecallback;

Public Class MqttconnectionImplements Mqttsimplecallback {
Imqttclient mqttclient = Null ;
Private Static Int Mqtt_port = 1883;
Private Static Mqttpersistence mqtt_persistence = Null ;
Public Static String mqtt_client_id = "";
Private Static Boolean Mqtt_clean_start = True ;
Private Static Short Mqtt_keep_alive = 60*15;
Private Static Int [] Mqtt_qualities_of_service = {0 };
Private Long Mstarttime;

Public Mqttconnection (string brokerhostname, string inittopic) Throws Mqttexception {
String mqttconnspec = "TCP: //" + brokerhostname + "@" + mqtt_port;
// Create the client and connect
Mqttclient = mqttclient. createmqttclient (mqttconnspec, mqtt_persistence );
String clientid = mqtt_client_id;
Mqttclient. Connect (clientid, mqtt_clean_start, mqtt_keep_alive );

//Register this client app has being able to receive messages
Mqttclient. registersimplehandler (This);

//Subscribe to an initial topic, which is combination of client ID and device ID.
Subscribetotopic (inittopic );

// Save Start Time
Mstarttime = system. currenttimemillis ();
// Star The keep-alives
// Startkeepalives ();
}

Private Void Subscribetotopic (string topicname) Throws Mqttexception {

If (Mqttclient = Null ) | (Mqttclient. isconnected () = False )){
// Quick sanity check-don't try and subscribe if we don't have
// A connection
System. Out. println ("subscribe to topic fail ");
} Else {
String [] topics = {topicname };
Mqttclient. subscribe (topics, mqtt_qualities_of_service );
}
}

Public Void Disconnect (){
Try {
Mqttclient. Disconnect ();
} Catch (Mqttpersistenceexception e ){
System. Out. println ("disconnection to server error ");
}
}


@ Override
Public VoidConnectionlost ()ThrowsException {
//Todo auto-generated method stub
System. Out. println ("connection to server closed ");
}

@ Override
Public Void Publisharrived (string topicname,Byte [] Payload, Int QoS, Boolean Retained)
Throws Exception {
// Todo auto-generated method stub
String S = New String (payload );
System. Out. println ("push message recived:" + S );
}
}

 

 

3.run the client program and use mosquitto_pub.exe-Q [QoS level]-T [Topic]-M [published content] in the Command window for testing.

 

In addition, mosquitto uses the socket select model to support a limited number of client connections.

To support a higher concurrency, you can consider using the "Policy Server + mosquitto cluster" approach, on the other hand, we can consider replacing mosquitto with some mqtt servers implemented by Erlang (the last time we saw a similar publishing/subscription system, we could subscribe to a user broadcast to 40 k every second, which is awesome ).

 

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.