Joke Collection Website - Blessing messages - How to use androidpn to push Android mobile phone messages?

How to use androidpn to push Android mobile phone messages?

Because the current web project uses android mobile phone message push, after trying many ways, it is more convenient to use part of androidpn code to realize this function. After a simple stress test with tsung, it is proved that the framework can meet the needs of most simple applications.

Androidpn contains two packages: server and client. The server part can run as a server alone, or it can be embedded in the servlet of the web project to interact with other parts of the web project in tomcat environment. The simple usage of androidpn is introduced in many articles on the Internet, so I won't elaborate here. This article mainly discusses how to integrate androidpn with your own web program.

The main package structure of the server part is as follows:

Among them, org.androidpn.server.dao, org.androidpn.server.model and org.androidpn.server.service use hibernate to link databases and realize simple user login authentication, which can be replaced by our own authentication module in development. The remaining packages are the main implementation of push.

Next, look at each package:

The class in the 1.util package is used to load the configuration file in the resource, where you can specify the listening port and ssl certificate directory.

2.org.androidpn.server.xmpp package defines some exception types, mainly including the entry class XmppServer, which is used to start and stop server programs.

Package 3.org.Android pn.server.xmpp.auth contains some authentication classes, and our own authentication module can be combined with androidpn here.

4.org. androidpn.server.xmpp.codec is an XML file parsing package of xmpp protocol. Messages received and sent by the server are encoded and decoded by this package.

The package 5.org.androidpn.server.xmpp.handler mainly processes messages, and we can define our own handlers for different message types.

6.org.androidpn.server.xmpp.net package is responsible for maintaining the persistent connection with the client and implementing some transmission modes for sending xmpp messages.

7.org.androidpn.server.xmpp.presence only contains the PresenceManager class, which is used to maintain the online status of the client.

The NotificationManager class in the 8.org.androidpn.server.xmpp.push package contains an interface to send messages to the client.

The 9.org.androidpn.server.xmpp.router package is responsible for sending the received data packet to the corresponding handler for processing. It is a routing package.

The package10.org.androidpn.server.xmpp.session defines the sessions used to represent persistent links, and each session contains the status information of the connection.

11.org.androidpn.server.xmpp.ssl is a toolkit for SSL authentication of connections.

The whole process of sending messages by the server is mainly:

1 Call NotificationManager.

2. Use sessionManager to find the corresponding client link in the current session collection.

3. Define your own XMPP message format and assemble it.

4. Send a message to the client through the corresponding session.

In this process, what we need to modify is step 3, that is, we need to define and assemble our own xmpp messages, so as to facilitate the transmission of appropriate information to the client and facilitate the parsing of the client. A simple example of message assembly is as follows:

Private IQ createMessageIQ (string title, string message, string user Id,

String JSON ){

element notification = document helper . createelement(qname . get(

“message“,INQURIE _ NAMESPACE));

notification . addelement(“title“)。 SetText (title);

Notification.addElement ("text"). SetText (message);

notification . addelement(“userId“)。 setText(userId);

notification . addelement(“JSON“)。 setText(JSON);

IQ IQ = new IQ ();

IQ . settype(IQ。 type . set);

Iq.setChildElement (notice);

Return to IQ;

}

It should be noted that when creating an element, the namespace passed in should match the namespace used for client resolution.

The process of receiving and processing messages on the server side is as follows:

1.connection receives the packet and decodes it using tsc.push.server.xmpp.codec

2. The router routes the packet to the corresponding processor according to the information (such as the namespace of the packet).

3.handler handles it.

The corresponding router and handler classes have examples for reference in androidpn, so the code will not be published here. In development, you only need to define your own router and handler class according to the message format sent by the client, and then register the router in PacketRouter and the handler in IQRouter.

The main package structure of the client part is as follows:

This side includes the functions of sending and receiving messages, parsing and starting and reconnecting persistent connections, which is very powerful. When we develop, we don't have to worry about the underlying connection at all, and we can focus on the development of the business part.

At the same time, the code structure is simple. In addition to android's service and broadcast classes, as well as some tool classes and constant classes:

1.NotificationIQ, NotificationIQ provider and NotificationPacket listener are responsible for parsing and processing received messages in notification format.

2.XmppManager is the main controller, and NotificationService maintains androidpn connection in the background through this class.

3.PersistentConnectionListener, PhoneStateChangeListener and ReconnectionThread.java are responsible for monitoring the status of mobile phones and performing disconnection and reconnection.

When we customize the message, we need to define three classes: define the entity of the message in ***IQ, convert the message into ***IQ entity in * * IQ provider, and process the entity in ***PacketListener. Please refer to three classes for specific implementation: notification IQ, notification IQ provider and notification packet listener. After defining these classes, you need to register these three classes with connection in XmppManager. The code is as follows:

//Connect task

connection . connect();

Log. I(log tag, "XMPP connection succeeded");

//Packet provider

ProviderManager.getInstance()。 addiq provider(“message“,

Constant. Notification namespace,

new notification IQ provider());

//LoginTask

//Packet filter

packet filter packet filter = new packet type filter(

notification IQ . class);

//Packet Listener

packet listener packet listener = xmpp manager

. getNotificationPacketListener();

connection . addpacketlistener(packet listener,packet filter);

It should be noted that when registering ***IQProvider, the namespace passed in needs to be consistent with the namespace used by the server when assembling messages, so as to receive messages correctly.