androidpn-tomcat服务器端
类文件:
org.androidpn.server.xmpp.push.NotificationManager
public void sendUpdateCommandToUser(String username,String version,String url) {
log.debug("sendUpdateCommandToUser()...");
IQ notificationIQ = createCommandIQ(version,url);
ClientSession session = sessionManager.getSession(username);
if (session != null) {
if (session.getPresence().isAvailable()) {
notificationIQ.setTo(session.getAddress());
session.deliver(notificationIQ);
}
}
}
private IQ createCommandIQ(String version,String url) {
Random random = new Random();
String id = Integer.toHexString(random.nextInt());
Element notification = DocumentHelper.createElement(QName.get(
"command", UPDATE_NAMESPACE));
notification.addElement("id").setText(id);
notification.addElement("version").setText(version);
notification.addElement("url").setText(url);
IQ iq = new IQ();
iq.setType(IQ.Type.set);
iq.setChildElement(notification);
return iq;
}
注:
UPDATE_NAMESPACE 自定义命名空间,也就是
xmlns="giec:iq:update" 这个里面的
主要增加两个方法:
sendUpdateCommandToUser发送消息给用户,创建自定义IQ:
createCommandIQ
生成消息格式:
androidpn-clinet客户端解析自定义消息:
主要增加3个类文件,自定义IQ类,IQ解析类
Provider,
packet
监听类(我这里只增加两个类,监听用原来的)
CommandIQ
public
class CommandIQ
extends IQ {
private String id;
private String version;
private String url;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append( "<").append( "update").append( " xmlns=\"").append(
"giec:iq:update").append( "\">");
if (id != null) {
buf.append( "" ).append(id).append(
"");
}
buf.append( "").append( "update").append( "> ");
return buf.toString();
}
}
private String id;
private String version;
private String url;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append( "<").append( "update").append( " xmlns=\"").append(
"giec:iq:update").append( "\">");
if (id != null) {
buf.append( "
}
buf.append( "").append( "update").append( "> ");
return buf.toString();
}
}
CommandIQProvider
public
class CommandIQProvider
implements IQProvider {
@Override
public IQ parseIQ(XmlPullParser parser) throws Exception {
CommandIQ command = new CommandIQ();
for ( boolean done = false; !done;) {
int eventType = parser.next();
if (eventType == 2) {
if ( "id".equals(parser.getName())) {
command.setId(parser.nextText());
}
if ( "version".equals(parser.getName())) {
command.setVersion(parser.nextText());
}
if ( "url".equals(parser.getName())) {
command.setUrl(parser.nextText());
}
} else if (eventType == 3
&& "command".equals(parser.getName())) {
done = true;
}
}
return command;
}
}
@Override
public IQ parseIQ(XmlPullParser parser) throws Exception {
CommandIQ command = new CommandIQ();
for ( boolean done = false; !done;) {
int eventType = parser.next();
if (eventType == 2) {
if ( "id".equals(parser.getName())) {
command.setId(parser.nextText());
}
if ( "version".equals(parser.getName())) {
command.setVersion(parser.nextText());
}
if ( "url".equals(parser.getName())) {
command.setUrl(parser.nextText());
}
} else if (eventType == 3
&& "command".equals(parser.getName())) {
done = true;
}
}
return command;
}
}
修改
org.androidpn.client.
XmppManager类文件
310行下面增加:
ProviderManager.getInstance().addIQProvider("command",
Constants.
UPDATE_NAMESPACE
,
new
CommandIQProvider());
注:
Constants.
UPDATE_NAMESPACE 这个命名空间必须和服务器上的一致
445行
PacketFilter packetFilter = new PacketTypeFilter(
NotificationIQ
.
class
);
将
NotificationIQ.class修改成IQ.class,接受任何类型的消息
上面完成后就可以在
NotificationPacketListener中解析消息体,并做自己想做的事了。