简介
我们以开源项目androidpn为例:
androidpn (Android Push Notification)是一个基于XMPP协议的java开源Android push notification实现。它包含了完整的客户端和服务器端。
androidpn包括Server端和Client端,项目名称是androidpn-server和androidpn-client。
事实上,androidpn-server可以支持app运行于iOS,UWP,Windows,Linux等平台,不仅限于android,因此,希望项目的名称改为XPN(Xmpp Push Notification)似乎更加符合其实际场景,我们以后涉及到Android Push Notification统称为XPN。
XNP目前状态
项目自2014年1月就已经停止更新了,此外,asmack项目也停止更新了,作者建议使用openfire官方的smack4.0,不过这样的话引入的jar会特别多,特别大。当然,我们下载到了asmack8.10.0比较新的稳定版本,可以完全用于学习和扩展。
项目相关下载站点
asmack-github.com – asmack项目地址
asmack-asmack.freakempire.de – asmack镜像地址
androidpn(XPN)-github.com – androidpn下载地址
一.关于Packet数据包
Packet是IQ,Message,Presence的父类,用于实现消息组件类型。
消息语义学message
message是一种基本推送消息方法,它不要求响应。主要用于IM、groupChat、alert和notification之类的应用中。
主要 属性如下:
type属性,它主要有5种类型:
normal:类似于email,主要特点是不要求响应;
chat:类似于qq里的好友即时聊天,主要特点是实时通讯;
groupchat:类似于聊天室里的群聊;
headline:用于发送alert和notification;
error:如果发送message出错,发现错误的实体会用这个类别来通知发送者出错了;
to属性:标识消息的接收方。
from属性:指发送方的名字或标示。为防止地址外泄,这个地址通常由发送者的server填写,而不是发送者。
载荷(payload):例如body,subject
<message to="lily@jabber.org/contact"
type="chat" >
<body> 你好,在忙吗</body>
</message>
出席信息语义学presence
presence用来表明用户的状态,如:online、away、dnd(请勿打扰)等。当改变自己的状态时,就会在stream的上下文中插入一个Presence元素,来表明自身的状态。要想接受presence消息,必须经过一个叫做presence subscription的授权过程。
属性:
type属性,非必须。有以下类别
subscribe:订阅其他用户的状态
probe:请求获取其他用户的状态
unavailable:不可用,离线(offline)状态
to属性:标识消息的接收方。
from属性:指发送方的名字或标示。
载荷(payload):
show:
chat:聊天中
away:暂时离开
xa:eXtend Away,长时间离开
dnd:勿打扰
status:格式自由,可阅读的文本。也叫做rich presence或者extended presence,常用来表示用户当前心情,活动,听的歌曲,看的视频,所在的聊天室,访问的网页,玩的游戏等等。
priority:范围-128~127。高优先级的resource能接受发送到bare JID的消息,低优先级的resource不能。优先级为
<presence from="alice@wonderland.lit/pda">
<show>xa</show>
<status>down the rabbit hole!</status>
</presence>
IQ语义学
一种请求/响应机制,从一个实体从发送请求,另外一个实体接受请求,并进行响应。例如,client在stream的上下文中插入一个元素,向Server请求得到自己的好友列表,Server返回一个,里面是请求的结果。
主要的属性是type。包括:
Get :获取当前域值。类似于http get方法。
Set :设置或替换get查询的值。类似于http put方法。
Result :说明成功的响应了先前的查询。类似于http状态码200。
Error: 查询和响应中出现的错误。
<iq from="alice@wonderland.lit/pda"
id="rr82a1z7"
to="alice@wonderland.lit"
type="get">
<query xmlns="jabber:iq:roster"/>
</iq>
二.自定义Packet
由于服务器和客户端使用的Packet不同,但是他们交互的数据格式都是xml,因此,这个过程我们理解xml实现过程即可。
1.定义Packet封装对象
由于asmack标签解析的限制,我们不能自定义解析,除非修改源码,这里出于简单,这里只能继承现有标签之一。
我么按照项目代码NotificationIQ为例,这里没有继承Packet,而是继承了IQ
import org.jivesoftware.smack.packet.IQ;
/**
* This class represents a notifcatin IQ packet.
*
* @author Sehwan Noh (devnoh@gmail.com)
*/
public class NotificationIQ extends IQ {
private String id;
private String apiKey;
private String title;
private String message;
private String uri;
public NotificationIQ() {
}
@Override
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append("notification").append(" xmlns=\"").append(
"androidpn:iq:notification").append("\">");
if (id != null) {
buf.append("<id>").append(id).append("</id>");
}
buf.append("</").append("notification").append("> ");
return buf.toString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUri() {
return uri;
}
public void setUri(String url) {
this.uri = url;
}
}
其中,getChildElementXml()是IQ的子类,用来拼接成<iq>下的直接点。
public abstract class IQ extends Packet {
private Type type = Type.GET;
public IQ() {
super();
}
public IQ(IQ iq) {
super(iq);
type = iq.getType();
}
/**
* Returns the type of the IQ packet.
*
* @return the type of the IQ packet.
*/
public Type getType() {
return type;
}
/**
* Sets the type of the IQ packet.
*
* @param type the type of the IQ packet.
*/
public void setType(Type type) {
if (type == null) {
this.type = Type.GET;
}
else {
this.type = type;
}
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<iq ");
if (getPacketID() != null) {
buf.append("id=\"" + getPacketID() + "\" ");
}
if (getTo() != null) {
buf.append("to=\"").append(StringUtils.escapeForXML(getTo())).append("\" ");
}
if (getFrom() != null) {
buf.append("from=\"").append(StringUtils.escapeForXML(getFrom())).append("\" ");
}
if (type == null) {
buf.append("type=\"get\">");
}
else {
buf.append("type=\"").append(getType()).append("\">");
}
// Add the query section if there is one.
String queryXML = getChildElementXML();
if (queryXML != null) {
buf.append(queryXML);
}
// Add the error sub-packet, if there is one.
XMPPError error = getError();
if (error != null) {
buf.append(error.toXML());
}
buf.append("</iq>");
return buf.toString();
}
/**
* Returns the sub-element XML section of the IQ packet, or <tt>null</tt> if there
* isn't one. Packet extensions <b>must</b> be included, if any are defined.<p>
*
* Extensions of this class must override this method.
*
* @return the child element section of the IQ XML.
*/
public abstract String getChildElementXML();
/**
* Convenience method to create a new empty {@link Type#RESULT IQ.Type.RESULT}
* IQ based on a {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}
* IQ. The new packet will be initialized with:<ul>
* <li>The sender set to the recipient of the originating IQ.
* <li>The recipient set to the sender of the originating IQ.
* <li>The type set to {@link Type#RESULT IQ.Type.RESULT}.
* <li>The id set to the id of the originating IQ.
* <li>No child element of the IQ element.
* </ul>
*
* @param iq the {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET} IQ packet.
* @throws IllegalArgumentException if the IQ packet does not have a type of
* {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}.
* @return a new {@link Type#RESULT IQ.Type.RESULT} IQ based on the originating IQ.
*/
public static IQ createResultIQ(final IQ request) {
if (!(request.getType() == Type.GET || request.getType() == Type.SET)) {
throw new IllegalArgumentException(
"IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML());
}
final IQ result = new IQ() {
public String getChildElementXML() {
return null;
}
};
result.setType(Type.RESULT);
result.setPacketID(request.getPacketID());
result.setFrom(request.getTo());
result.setTo(request.getFrom());
return result;
}
/**
* Convenience method to create a new {@link Type#ERROR IQ.Type.ERROR} IQ
* based on a {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}
* IQ. The new packet will be initialized with:<ul>
* <li>The sender set to the recipient of the originating IQ.
* <li>The recipient set to the sender of the originating IQ.
* <li>The type set to {@link Type#ERROR IQ.Type.ERROR}.
* <li>The id set to the id of the originating IQ.
* <li>The child element contained in the associated originating IQ.
* <li>The provided {@link XMPPError XMPPError}.
* </ul>
*
* @param iq the {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET} IQ packet.
* @param error the error to associate with the created IQ packet.
* @throws IllegalArgumentException if the IQ packet does not have a type of
* {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}.
* @return a new {@link Type#ERROR IQ.Type.ERROR} IQ based on the originating IQ.
*/
public static IQ createErrorResponse(final IQ request, final XMPPError error) {
if (!(request.getType() == Type.GET || request.getType() == Type.SET)) {
throw new IllegalArgumentException(
"IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML());
}
final IQ result = new IQ() {
public String getChildElementXML() {
return request.getChildElementXML();
}
};
result.setType(Type.ERROR);
result.setPacketID(request.getPacketID());
result.setFrom(request.getTo());
result.setTo(request.getFrom());
result.setError(error);
return result;
}
/**
* A class to represent the type of the IQ packet. The types are:
*
* <ul>
* <li>IQ.Type.GET
* <li>IQ.Type.SET
* <li>IQ.Type.RESULT
* <li>IQ.Type.ERROR
* </ul>
*/
public static class Type {
public static final Type GET = new Type("get");
public static final Type SET = new Type("set");
public static final Type RESULT = new Type("result");
public static final Type ERROR = new Type("error");
/**
* Converts a String into the corresponding types. Valid String values
* that can be converted to types are: "get", "set", "result", and "error".
*
* @param type the String value to covert.
* @return the corresponding Type.
*/
public static Type fromString(String type) {
if (type == null) {
return null;
}
type = type.toLowerCase();
if (GET.toString().equals(type)) {
return GET;
}
else if (SET.toString().equals(type)) {
return SET;
}
else if (ERROR.toString().equals(type)) {
return ERROR;
}
else if (RESULT.toString().equals(type)) {
return RESULT;
}
else {
return null;
}
}
private String value;
private Type(String value) {
this.value = value;
}
public String toString() {
return value;
}
}
}
最终可生成如下结构的数据
<iq from="">
<nofitication xlns="">
<iq>
我们在项目中的使用很简单
xmppManager.getConnection().sendPacket(<NotificationIQ>niq)
当然,上面只是实现了object->xml,接下来我们实现xml->data
2.实现IQProvider
先来看看IQProvider源码
public interface IQProvider {
/**
* Parse the IQ sub-document and create an IQ instance. Each IQ must have a
* single child element. At the beginning of the method call, the xml parser
* will be positioned at the opening tag of the IQ child element. At the end
* of the method call, the parser <b>must</b> be positioned on the closing tag
* of the child element.
*
* @param parser an XML parser.
* @return a new IQ instance.
* @throws Exception if an error occurs parsing the XML.
*/
public IQ parseIQ(XmlPullParser parser) throws Exception;
}
实现自定义的解析工具
public class NotificationIQProvider implements IQProvider {
public NotificationIQProvider() {
}
@Override
public IQ parseIQ(XmlPullParser parser) throws Exception {
NotificationIQ notification = new NotificationIQ();
for (boolean done = false; !done;) {
int eventType = parser.next();
if (eventType == 2) {
if ("id".equals(parser.getName())) {
notification.setId(parser.nextText());
}
if ("apiKey".equals(parser.getName())) {
notification.setApiKey(parser.nextText());
}
if ("title".equals(parser.getName())) {
notification.setTitle(parser.nextText());
}
if ("message".equals(parser.getName())) {
notification.setMessage(parser.nextText());
}
if ("uri".equals(parser.getName())) {
notification.setUri(parser.nextText());
}
} else if (eventType == 3
&& "notification".equals(parser.getName())) {
done = true;
}
}
return notification;
}
}
项目中使用方法
ProviderManager.getInstance().addIQProvider("notification",
"androidpn:iq:notification",
new NotificationIQProvider());
在asmack中PacketParserUtils类中会进行如下调用
Object provider = ProviderManager.getInstance().getIQProvider(elementName, namespace);
if (provider != null) {
if (provider instanceof IQProvider) {
iqPacket = ((IQProvider)provider).parseIQ(parser);
}
else if (provider instanceof Class) {
iqPacket = (IQ)PacketParserUtils.parseWithIntrospection(elementName,
(Class<?>)provider, parser);
}
}