public boolean sendMessage(MessageData... messages) { boolean msgSent; try { for (MessageData message : messages) { String content = message.createMessageString(); logger.info("Sending: " + content); HttpURLConnection connection = (HttpURLConnection) MSG_SERVICE_URL.openConnection(); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); connection.setRequestProperty("Content-Length", Integer.toString(content.length())); connection.setRequestProperty("Authorization", "GoogleLogin auth=" + getToken()); OutputStream out = connection.getOutputStream(); out.write(content.getBytes("UTF-8")); out.close(); logger.info("Response: " + connection.getResponseCode()); handleUpdatedTokenResponse(connection.getHeaderField(HEADER_FIELD_UPDATED_TOKEN)); } msgSent = true; } catch (IOException e) { e.printStackTrace(); msgSent = false; } return msgSent; }
/** * This accepts a message and persists it in the AppEngine datastore, it will also broadcast the * message to upto 10 registered android devices via Google Cloud Messaging * * @param message the entity to be inserted. * @return * @throws IOException */ @ApiMethod(name = "sendMessage") public void sendMessage(@Named("message") String message) throws IOException { Sender sender = new Sender(API_KEY); // create a MessageData entity with a timestamp of when it was // received, and persist it MessageData messageObj = new MessageData(); messageObj.setMessage(message); messageObj.setTimestamp(System.currentTimeMillis()); EntityManager mgr = getEntityManager(); try { mgr.persist(messageObj); } finally { mgr.close(); } // ping a max of 10 registered devices CollectionResponse<DeviceInfo> response = endpoint.listDeviceInfo(null, 10); for (DeviceInfo deviceInfo : response.getItems()) { doSendViaGcm(message, sender, deviceInfo); } }
/** * Respond to various requests. The first step is to create a response message by switching the * from and to fields of the incoming message. Then we get the action specified in the request and * handle the action accordingly. * * @param request */ public MessageData respond(String request) { MessageData req = new MessageData(request); MessageData jd = new MessageData(); String action = Session.responseMessage; jd.setValue("action", action); String from = getOwner().getSid(); String to = "undetermined"; if (!req.isValid()) { action = Session.invalidMessage; } to = req.getValue("from"); action = req.getValue("action"); jd.setValue("to", to); jd.setValue("from", from); // String actreq = req.getValue ("message"); if (action.equals(terminateRequest)) { jd.setValue("action", Session.terminateMessage); } else if (action.equals(uwhoRequest)) { jd.setValue("action", Session.responseMessage); String s = "i am " + getOwner().getId(); jd.setValue("message", s); } else if (action.equals(Session.responseMessage)) { jd.setValue("action", Session.finishedMessage); } else { jd.setValue("action", Session.invalidMessage); } return jd; }