Back to project page droidBBpush.
The source code is released under:
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...
If you think the Android project droidBBpush listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.arg3.examples.push; /* www. jav a2s . c o m*/ import com.arg3.examples.push.dao.DeviceDAO; import com.arg3.examples.push.model.Device; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.inject.Inject; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by ryan on 2014-09-25. */ public class SendServlet extends BaseServlet { @Inject DeviceDAO deviceDAO; @Inject BlackberryPushService bbService; @Inject AndroidPushService droidService; @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { String deviceId = req.getParameter("deviceId"); try { Device device = deviceDAO.find(Integer.valueOf(deviceId)); if (device == null) { addFlashError(req, "Device not found"); resp.sendRedirect("/push"); return; } req.setAttribute("device", device); } catch (SQLException e) { e.printStackTrace(); } req.getRequestDispatcher("/send_push.ftl").forward(req, resp); } @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { String deviceId = req.getParameter("deviceId"); String message = req.getParameter("message"); if (message == null || message.isEmpty()) { addFlashError(req, "must provide a message"); resp.sendRedirect("/send?deviceId=" + deviceId); return; } boolean success; if (deviceId == null || deviceId.isEmpty()) { success = sendMessageToAll(message); } else { success = sendMessageToDevice(deviceId, message); } if (success) addFlashMessage(req, "Message sent successfully"); else addFlashError(req, "Unable to send message(s) at this time"); resp.sendRedirect("/push"); } private boolean sendMessageToAll(String message) throws ServletException { List<Device> devices; try { devices = deviceDAO.findAllDevices(); } catch (SQLException e) { throw new ServletException("Unable to query devices", e); } List<String> androidTokens = new ArrayList<String>(); List<String> bbTokens = new ArrayList<String>(); for (Device device : devices) { switch (device.getType()) { case Blackberry: bbTokens.add(device.getToken()); break; case Android: androidTokens.add(device.getToken()); break; } } boolean success = true; if (androidTokens.size() > 0) { success = droidService.sendMessage(androidTokens, message) && success; } if (bbTokens.size() > 0) { success = bbService.sendMessage(bbTokens, message) && success; } return success; } private boolean sendMessageToDevice(String deviceId, String message) throws ServletException { Device device = null; try { device = deviceDAO.find(Integer.valueOf(deviceId)); } catch (SQLException e) { e.printStackTrace(); } if (device == null) { throw new ServletException("Device " + deviceId + " not found"); } switch (device.getType()) { case Blackberry: return bbService.sendMessage(device, message); case Android: return droidService.sendMessage(device, message); default: throw new ServletException("Unknown device type"); } } }