androidpn的学习研究(三)androidpn-server服务端几个类说明

介绍AndroidPN推送通知系统的实现原理,包括服务器端组件如SessionManager、AuthManager等的功能,以及客户端如何通过XMPP协议进行注册和接收消息。

 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源代码:

Java代码   收藏代码
  1. public class NotificationManager {  
  2.   
  3.     private static final String NOTIFICATION_NAMESPACE = "androidpn:iq:notification";  
  4.   
  5.     private final Log log = LogFactory.getLog(getClass());  
  6.   
  7.     private SessionManager sessionManager;  
  8.   
  9.     /** 
  10.      * Constructor. 
  11.      */  
  12.     public NotificationManager() {  
  13.         sessionManager = SessionManager.getInstance();  
  14.     }  
  15.   
  16.     /** 
  17.      * Broadcasts a newly created notification message to all connected users. 
  18.      *  
  19.      * @param apiKey the API key 
  20.      * @param title the title 
  21.      * @param message the message details 
  22.      * @param uri the uri 
  23.      */  
  24.     public void sendBroadcast(String apiKey, String title, String message,  
  25.             String uri) {  
  26.         log.debug("sendBroadcast()...");  
  27.         IQ notificationIQ = createNotificationIQ(apiKey, title, message, uri);  
  28.         for (ClientSession session : sessionManager.getSessions()) {  
  29.             if (session.getPresence().isAvailable()) {  
  30.                 notificationIQ.setTo(session.getAddress());  
  31.                 session.deliver(notificationIQ);  
  32.             }  
  33.         }  
  34.     }  
  35.   
  36.     /** 
  37.      * Sends a newly created notification message to the specific user. 
  38.      *  
  39.      * @param apiKey the API key 
  40.      * @param title the title 
  41.      * @param message the message details 
  42.      * @param uri the uri 
  43.      */  
  44.     public void sendNotifcationToUser(String apiKey, String username,  
  45.             String title, String message, String uri) {  
  46.         log.debug("sendNotifcationToUser()...");  
  47.         IQ notificationIQ = createNotificationIQ(apiKey, title, message, uri);  
  48.         ClientSession session = sessionManager.getSession(username);  
  49.         if (session != null) {  
  50.             if (session.getPresence().isAvailable()) {  
  51.                 notificationIQ.setTo(session.getAddress());  
  52.                 session.deliver(notificationIQ);  
  53.             }  
  54.         }  
  55.     }  
  56.   
  57.     /** 
  58.      * Creates a new notification IQ and returns it. 
  59.      */  
  60.     private IQ createNotificationIQ(String apiKey, String title,  
  61.             String message, String uri) {  
  62.         Random random = new Random();  
  63.         String id = Integer.toHexString(random.nextInt());  
  64.         // String id = String.valueOf(System.currentTimeMillis());  
  65.   
  66.         Element notification = DocumentHelper.createElement(QName.get(  
  67.                 "notification", NOTIFICATION_NAMESPACE));  
  68.         notification.addElement("id").setText(id);  
  69.         notification.addElement("apiKey").setText(apiKey);  
  70.         notification.addElement("title").setText(title);  
  71.         notification.addElement("message").setText(message);  
  72.         notification.addElement("uri").setText(uri);  
  73.   
  74.         IQ iq = new IQ();  
  75.         iq.setType(IQ.Type.set);  
  76.         iq.setChildElement(notification);  
  77.   
  78.         return iq;  
  79.     }  
  80. }  

 

其中:

    发布订阅式的发送消息调用方法:

    /**
     * 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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值