/** * 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()..."); List<User> users = userService.getUsers(); for (User user : users) { ClientSession session = sessionManager.getSession(user.getUsername()); Random random = new Random(); String id = Integer.toHexString(random.nextInt()); IQ notificationIQ = createNotificationIQ(id, apiKey, title, message, uri); if (session != null && session.getPresence().isAvailable()) { notificationIQ.setTo(session.getAddress()); session.deliver(notificationIQ); } saveNotification(id, apiKey, user.getUsername(), title, message, uri); } }
/** * Delivers the packet to the packet recipient. * * @param packet the packet to deliver * @throws PacketException if the packet is null or the recipient was not found. */ public static void deliver(Packet packet) throws PacketException { if (packet == null) { throw new PacketException("Packet was null"); } try { JID recipient = packet.getTo(); if (recipient != null) { ClientSession clientSession = SessionManager.getInstance().getSession(recipient); if (clientSession != null) { clientSession.deliver(packet); } } } catch (Exception e) { log.error("Could not deliver packet: " + packet.toString(), e); } }
/** * 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, boolean save) { log.debug("sendNotifcationToUser()..."); Random random = new Random(); String id = Integer.toHexString(random.nextInt()); IQ notificationIQ = createNotificationIQ(id, apiKey, title, message, uri); ClientSession session = sessionManager.getSession(username); if (session != null) { if (session.getPresence().isAvailable()) { notificationIQ.setTo(session.getAddress()); session.deliver(notificationIQ); } } try { User user = userService.getUserByUsername(username); if (user != null && save) { saveNotification(id, apiKey, username, title, message, uri); } } catch (UserNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }