The perfect solution for Android message push

The push function is becoming more and more important in the development of mobile phone applications, and has become a must for mobile phone development. In Android application development, due to well-known reasons, we have to spend a lot of time on Android message push. This article is used to discuss with you a perfect solution for Android message push.

  1. The basics of message push

         Message push is a new technology that reduces information overload by regularly transmitting the information that users need on the Internet. Push technology reduces the time spent searching on the web by automatically delivering information to users. It searches and filters information according to users' interests, and pushes it to users regularly to help users discover valuable information efficiently

When we develop mobile applications that need to interact with the server, we basically need to interact with the server, including uploading data to the server and getting data from the server at the same time.

Under normal circumstances, the client is active in communication between the client and the server, but there is a problem that once the server data is updated or the server wants to send a notification to the client, it can only be realized when the client is connected. In this way, the message loses its real-time character.

How to enable the client to receive messages and notifications from the server in real time? Generally speaking, there are two ways. The first is that the client uses the Pull (pull) method, which is to go to the server to obtain information from time to time. Whether any updated information appears. The second is that the server uses the Push method. When there is new information on the server side, it pushes the latest information to the client side. In this way, the client can automatically receive the message.

  Although both methods of Pull and Push can achieve the function of obtaining server-side update information, it is obvious that the Push method is superior to the Pull method. Because the Pull method consumes more network traffic on the client side, and more importantly, consumes electricity, we also need our program to constantly monitor changes on the server side.  

  2. Several common solution implementation principles

  1 ) Polling (Pull) method: The client sends an inquiry message to the server periodically, and the message is synchronized immediately once the server changes.

  2 ) SMS (Push) method: By intercepting the SMS message and parsing the message content to understand the server's command, but this method is generally difficult for users to bear economically.

 3 ) Persistent connection (Push) method: establish a long-term connection between the client and the server, so that the timely and real-time performance of the message can be realized.

  3. Overview of message push solutions

  A. C2DM cloud push solution

On the Android mobile phone platform, Google provides C2DM ( Cloudto Device Messaging ) service. Android Cloud to Device Messaging (C2DM) is a service that helps developers send data from servers to Android applications. The service provides a simple, lightweight mechanism that allows the server to notify mobile applications to communicate directly with the server in order to obtain application updates and user data from the server.

The main problem with this solution is that C2DM needs to rely on the official C2DM server provided by Google , which is often unavailable due to the domestic network environment.

  B. MQTT protocol realizes Android push

  Using the MQTT protocol to implement the Android push function is also a solution. MQTT is a lightweight message publish / subscribe protocol, which is an ideal solution for implementing a message push server based on mobile phone clients.

  wmqtt.jar is an implementation of the MQTT protocol provided by IBM . We can download the sample code of the project from here ( https://github.com/tokudu/AndroidPushNotificationsDemo ), and can find a server-side implementation written in PHP ( https://github.com/tokudu/PhpMQTTClient ).

  C. RSMB realizes the push function

  Really Small Message Broker (RSMB) , is a simple MQTT broker, also provided by IBM , its viewing address is: http://www.alphaworks.ibm.com/tech/rsmb . By default , port 1883 is opened. In the 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 this http://pecl.php.net/package/sam/download/0.2.0 address .

   D , XMPP protocol to achieve Android push

  The bottom layer of Google 's official C2DM server is also encapsulated using the XMPP protocol. XMPP ( Extensible Messaging and Presentation Protocol ) is an Extensible Markup Language ( XML ) based protocol 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.

  androidpn is a java open source Android push notification implementation based on XMPP protocol . It contains a complete client and server side. But there are also some shortcomings:

  1 ) For example, if the time is too long, the push information will no longer be received.

  2 ) The performance is also not stable enough.

3 ) If the message is pushed out from the server, it is no longer managed, no matter whether the message reaches the client phone successfully or not.

If we want to use androidpn , we need to do a lot of work, we need to understand the XMPP protocol, understand the implementation mechanism of Androidpn , and we need to debug the internal bugs .

  E. Using third-party platforms

  At present, there are some push platforms available at home and abroad, but when it comes to charging issues, confidentiality issues, service quality issues, expansion issues, etc., we have to stay away.

         4. The perfect solution for message push

      Based on the above discussion, there are many schemes for establishing Android message push, but each scheme has its own advantages and disadvantages. But in any case, it is the best policy to build a push platform by yourself. Because you have it, he has it as much as he has it.

         It is recommended to use the "Jurisheng Android Message Push Component" ( http://www.bjjrs.net/product/13629681868537.html ) when building its own push platform. The group can not only use it out-of-the-box, but also provide source code for extension to achieve its own special needs.

       A , push principle

    Jiurisheng Android message push component implements Android push based on XMPP protocol . XMPP (Extensible Messaging and Presentation Protocol) is an Extensible Markup Language ( XML ) based protocol 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.

    The implementation principle of Jiurisheng Android message push component is shown in the following figure :

            <IGNORE_JS_OP>a.JPG

 

 

                                                  Figure 1 - Schematic diagram of message push

    Jiurisheng Android message push component consists of server part and client part. Each part consists of XMPP protocol components and external interface components. The XMPP protocol component is responsible for connection management and message communication between the server and the Android client, and the external interface component is responsible for receiving commands from the application system and client applications, and sending received notification messages to the application system.

    Jiurisheng Android messaging component provides Tomcat -based server applications and Android development jar packages. The Tomcat -based server application can be deployed directly on Tomcat , and the Android development jar package can be imported into the Android project.

   B integration method

    1 ), server deployment

    The server application of Tomcat , the Android messaging component of Jiurisheng , is directly deployed in Tomcat , and the port number can be set arbitrarily.

    2 ), client jar package reference

    Create a libs directory in the Android project , and then copy the provided Android development jar package to this directory. See below:

                                                 <IGNORE_JS_OP>b.JPG

 

                                                                             

Figure 2-jar package import diagram

    3 ), Android project AndroidManifest.xml file modification

  Add the following permissions to this file:

     <uses-permission android:name="android.permission.READ_PHONE_STATE" />

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

     <uses-permission android:name="android.permission.INTERNET" />

     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

     <uses-permission android:name="android.permission.VIBRATE" />

   Register the service in this file:

     <service android:enabled="true"

     android:name="com.bjjrs.server.NotificationService"

     android:label="NotificationService">

          <intent-filter>

             <action android:name="com.bjjrs.server.NotificationService" />

         </intent-filter>

    </service>

   So far, the integration of Jiurisheng 's Android message components has been completed.

    C. Interface method

    1 ) The server-side interface adopts the access method based on the http protocol, and uses the http protocol to obtain various information from the server to realize the push of notification messages.

Various user messages can be queried by using the following methods and parameters:

  http://localhost:8080/user.do?action=getAllUser&isOnline=&userID=&userType=&deptID=&deptName=&realName=

    The push of various messages can be achieved in the following ways:

    http://localhost:8080/notification.do?action=pushNoti&userNames=&title=&content=

    2 ) The Android client interface adopts the broadcasting mechanism.

    Message reception: When the XMPP protocol component receives a push message, it will broadcast the message in a certain format to notify other applications on the client side to receive and process the message.

    Message sending: When a client application needs to send an instant message to the server or other clients, it only needs to broadcast the message in a certain format, and the XMPP component will automatically receive the message and send it to other designated clients.

   D. Advantages

    1 ), the system integration is simple, no complicated settings are required.

    2 ) The Android client application and the Jiurisheng Android message push component are completely separated, and call each other through the interface to realize the optimization of the module application.

    3 ) The client communication mechanism adopts broadcast mode, which brings great flexibility and scalability to the client application, and can freely process the received push messages.

    4 ), Jiurisheng Android message push component has functions such as message storage, message retransmission, message routing, etc. on the server side, and has the functions of disconnection reconnection, reception confirmation, reading confirmation, message sending, command execution, etc. on the client side This function ensures that messages can be pushed to clients, and that clients can receive and read messages at the same time.

   E. Scope of application

  Jiurisheng Android message push component can be used in the following scenarios:

    1 ), for message push. Such as: notification, emergency command, etc.

    2 ), user instant message interaction. Such as online chat, work interaction, etc.

    3 ), for remote control. Such as controlling the status of the remote client, data reporting, etc.

 Finally, I hope that friends who reprint can respect the author's labor results and add the reprint address.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326965345&siteId=291194637