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

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

public class NotificationManager {

    private static final String NOTIFICATION_NAMESPACE = "androidpn:iq:notification";

    private final Log log = LogFactory.getLog(getClass());

    private SessionManager sessionManager;

    /**
     * Constructor.
     */
    public NotificationManager() {
        sessionManager = SessionManager.getInstance();
    }

    /**
     * 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) {
        log.debug("sendBroadcast()...");
        IQ notificationIQ = createNotificationIQ(apiKey, title, message, uri);
        for (ClientSession session : sessionManager.getSessions()) {
            if (session.getPresence().isAvailable()) {
                notificationIQ.setTo(session.getAddress());
                session.deliver(notificationIQ);
            }
        }
    }

    /**
     * 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) {
        log.debug("sendNotifcationToUser()...");
        IQ notificationIQ = createNotificationIQ(apiKey, title, message, uri);
        ClientSession session = sessionManager.getSession(username);
        if (session != null) {
            if (session.getPresence().isAvailable()) {
                notificationIQ.setTo(session.getAddress());
                session.deliver(notificationIQ);
            }
        }
    }

    /**
     * Creates a new notification IQ and returns it.
     */
    private IQ createNotificationIQ(String apiKey, String title,
            String message, String uri) {
        Random random = new Random();
        String id = Integer.toHexString(random.nextInt());
        // String id = String.valueOf(System.currentTimeMillis());

        Element notification = 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);

        IQ iq = new IQ();
        iq.setType(IQ.Type.set);
        iq.setChildElement(notification);

        return iq;
    }
}

其中:

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

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

猜你喜欢

转载自topmanopensource.iteye.com/blog/1608085