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

本文深入探讨了AndroidPN推送通知系统的实现原理,包括其基于XMPP协议的Java开源架构,详细介绍了服务器端和客户端的功能模块,以及消息处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. publicclassNotificationManager{
  2. privatestaticfinalStringNOTIFICATION_NAMESPACE="androidpn:iq:notification";
  3. privatefinalLoglog=LogFactory.getLog(getClass());
  4. privateSessionManagersessionManager;
  5. /**
  6. *Constructor.
  7. */
  8. publicNotificationManager(){
  9. sessionManager=SessionManager.getInstance();
  10. }
  11. /**
  12. *Broadcastsanewlycreatednotificationmessagetoallconnectedusers.
  13. *
  14. *@paramapiKeytheAPIkey
  15. *@paramtitlethetitle
  16. *@parammessagethemessagedetails
  17. *@paramuritheuri
  18. */
  19. publicvoidsendBroadcast(StringapiKey,Stringtitle,Stringmessage,
  20. Stringuri){
  21. log.debug("sendBroadcast()...");
  22. IQnotificationIQ=createNotificationIQ(apiKey,title,message,uri);
  23. for(ClientSessionsession:sessionManager.getSessions()){
  24. if(session.getPresence().isAvailable()){
  25. notificationIQ.setTo(session.getAddress());
  26. session.deliver(notificationIQ);
  27. }
  28. }
  29. }
  30. /**
  31. *Sendsanewlycreatednotificationmessagetothespecificuser.
  32. *
  33. *@paramapiKeytheAPIkey
  34. *@paramtitlethetitle
  35. *@parammessagethemessagedetails
  36. *@paramuritheuri
  37. */
  38. publicvoidsendNotifcationToUser(StringapiKey,Stringusername,
  39. Stringtitle,Stringmessage,Stringuri){
  40. log.debug("sendNotifcationToUser()...");
  41. IQnotificationIQ=createNotificationIQ(apiKey,title,message,uri);
  42. ClientSessionsession=sessionManager.getSession(username);
  43. if(session!=null){
  44. if(session.getPresence().isAvailable()){
  45. notificationIQ.setTo(session.getAddress());
  46. session.deliver(notificationIQ);
  47. }
  48. }
  49. }
  50. /**
  51. *CreatesanewnotificationIQandreturnsit.
  52. */
  53. privateIQcreateNotificationIQ(StringapiKey,Stringtitle,
  54. Stringmessage,Stringuri){
  55. Randomrandom=newRandom();
  56. Stringid=Integer.toHexString(random.nextInt());
  57. //Stringid=String.valueOf(System.currentTimeMillis());
  58. Elementnotification=DocumentHelper.createElement(QName.get(
  59. "notification",NOTIFICATION_NAMESPACE));
  60. notification.addElement("id").setText(id);
  61. notification.addElement("apiKey").setText(apiKey);
  62. notification.addElement("title").setText(title);
  63. notification.addElement("message").setText(message);
  64. notification.addElement("uri").setText(uri);
  65. IQiq=newIQ();
  66. iq.setType(IQ.Type.set);
  67. iq.setChildElement(notification);
  68. returniq;
  69. }
  70. }

其中:

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

/**
* 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、付费专栏及课程。

余额充值