AndroidPN(Android Push Notification) 是一个基于XMPP协议的Java开源推送通知实现,它包含了完整的客户端和服务端。
AndroidPN基于Openfire下的一些开源项目构建。
AndroidPN服务器包含两个部分,
一个是侦听在5222端口上的XMPP服务,负责与客户端的XMPPConnection类进行通信,作用是用户注册和身份认证,并发送推送通知消息。
另外一部分是Web服务器,采用一个轻量级的HTTP服务器,负责接收用户的Web请求。
最上层包含四个组成部分,分别是SessionManager,Auth Manager,PresenceManager以及Notification Manager。
SessionManager负责管理客户端与服务器之间的会话。
Auth Manager负责客户端用户认证管理。
Presence Manager负责管理客户端用户的登录状态。
NotificationManager负责实现服务器向客户端推送消息功能。
IQHandler消息处理器的类:
IQHandler:消息处理器抽象类。
IQAuthHandler:权限协议的消息处理类,消息的类型为:jabber:iq:auth
IQRegisterHandler:用户注册的消息处理类,消息类型为: jabber:iq:register
IQRosterHandler:用户消息交互类,消息类型为:jabber:iq:roster
PresenceUpdateHandler:用户状态展现变化处理类。内部调用,不具有类型。
NotificationManager源代码:
- publicclassNotificationManager{
- privatestaticfinalStringNOTIFICATION_NAMESPACE="androidpn:iq:notification";
- privatefinalLoglog=LogFactory.getLog(getClass());
- privateSessionManagersessionManager;
- /**
- *Constructor.
- */
- publicNotificationManager(){
- sessionManager=SessionManager.getInstance();
- }
- /**
- *Broadcastsanewlycreatednotificationmessagetoallconnectedusers.
- *
- *@paramapiKeytheAPIkey
- *@paramtitlethetitle
- *@parammessagethemessagedetails
- *@paramuritheuri
- */
- publicvoidsendBroadcast(StringapiKey,Stringtitle,Stringmessage,
- Stringuri){
- log.debug("sendBroadcast()...");
- IQnotificationIQ=createNotificationIQ(apiKey,title,message,uri);
- for(ClientSessionsession:sessionManager.getSessions()){
- if(session.getPresence().isAvailable()){
- notificationIQ.setTo(session.getAddress());
- session.deliver(notificationIQ);
- }
- }
- }
- /**
- *Sendsanewlycreatednotificationmessagetothespecificuser.
- *
- *@paramapiKeytheAPIkey
- *@paramtitlethetitle
- *@parammessagethemessagedetails
- *@paramuritheuri
- */
- publicvoidsendNotifcationToUser(StringapiKey,Stringusername,
- Stringtitle,Stringmessage,Stringuri){
- log.debug("sendNotifcationToUser()...");
- IQnotificationIQ=createNotificationIQ(apiKey,title,message,uri);
- ClientSessionsession=sessionManager.getSession(username);
- if(session!=null){
- if(session.getPresence().isAvailable()){
- notificationIQ.setTo(session.getAddress());
- session.deliver(notificationIQ);
- }
- }
- }
- /**
- *CreatesanewnotificationIQandreturnsit.
- */
- privateIQcreateNotificationIQ(StringapiKey,Stringtitle,
- Stringmessage,Stringuri){
- Randomrandom=newRandom();
- Stringid=Integer.toHexString(random.nextInt());
- //Stringid=String.valueOf(System.currentTimeMillis());
- Elementnotification=DocumentHelper.createElement(QName.get(
- "notification",NOTIFICATION_NAMESPACE));
- notification.addElement("id").setText(id);
- notification.addElement("apiKey").setText(apiKey);
- notification.addElement("title").setText(title);
- notification.addElement("message").setText(message);
- notification.addElement("uri").setText(uri);
- IQiq=newIQ();
- iq.setType(IQ.Type.set);
- iq.setChildElement(notification);
- returniq;
- }
- }
其中:
发布订阅式的发送消息调用方法:
/**
* Broadcasts a newly created notification message to all connected users.
*
* @param apiKey the API key
* @param title the title
* @param message the message details
* @param uri the uri
*/
public void sendBroadcast(String apiKey, String title, String message,
String uri);
点对点的发送消息调用方法:
/**
* Sends a newly created notification message to the specific user.
*
* @param apiKey the API key
* @param title the title
* @param message the message details
* @param uri the uri
*/
public void sendNotifcationToUser(String apiKey, String username,
String title, String message, String uri);
创建发送消息的方法:
/**
* Creates a new notification IQ and returns it.
*/
private IQ createNotificationIQ(String apiKey, String title,
String message, String uri);