private void doUnregister(String deviceRegistrationID, String accountName) { log.info("in unregister: accountName = " + accountName); PersistenceManager pm = PMF.get().getPersistenceManager(); try { List<DeviceInfo> registrations = DeviceInfo.getDeviceInfoForUser(accountName); for (int i = 0; i < registrations.size(); i++) { DeviceInfo deviceInfo = registrations.get(i); if (deviceInfo.getDeviceRegistrationID().equals(deviceRegistrationID)) { pm.deletePersistent(deviceInfo); // Keep looping in case of duplicates } } } catch (JDOObjectNotFoundException e) { log.warning("User " + accountName + " unknown"); } catch (Exception e) { log.warning("Error unregistering device: " + e.getMessage()); } finally { pm.close(); } }
private void doRegister( String deviceRegistrationId, String deviceType, String deviceId, String accountName) throws Exception { log.info("in register: accountName = " + accountName); PersistenceManager pm = PMF.get().getPersistenceManager(); try { List<DeviceInfo> registrations = DeviceInfo.getDeviceInfoForUser(accountName); log.info("got registrations"); if (registrations.size() > MAX_DEVICES) { log.info("got registrations > MAX_DEVICES"); // we could return an error - but user can't handle it yet. // we can't let it grow out of bounds. // TODO: we should also define a 'ping' message and expire/remove // unused registrations DeviceInfo oldest = registrations.get(0); if (oldest.getRegistrationTimestamp() == null) { pm.deletePersistent(oldest); } else { long oldestTime = oldest.getRegistrationTimestamp().getTime(); for (int i = 1; i < registrations.size(); i++) { if (registrations.get(i).getRegistrationTimestamp().getTime() < oldestTime) { oldest = registrations.get(i); oldestTime = oldest.getRegistrationTimestamp().getTime(); } } pm.deletePersistent(oldest); } } // Get device if it already exists, else create String suffix = (deviceId != null ? "#" + Long.toHexString(Math.abs(deviceId.hashCode())) : ""); log.info("suffix = " + suffix); Key key = KeyFactory.createKey(DeviceInfo.class.getSimpleName(), accountName + suffix); log.info("key = " + key); DeviceInfo device = null; try { device = pm.getObjectById(DeviceInfo.class, key); } catch (JDOObjectNotFoundException e) { log.info("Caught JDOObjectNotFoundException"); } if (device == null) { device = new DeviceInfo(key, deviceRegistrationId); device.setType(deviceType); } else { // update registration id device.setDeviceRegistrationID(deviceRegistrationId); device.setRegistrationTimestamp(new Date()); } pm.makePersistent(device); return; } catch (Exception e) { log.info("Caught exception: " + e); throw e; } finally { pm.close(); } }
public static String sendMessage(ServletContext context, String recipient, String message) { PersistenceManager pm = PMF.get().getPersistenceManager(); try { UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); String sender = "nobody"; if (user != null) { sender = user.getEmail(); } log.info("sendMessage: sender = " + sender); log.info("sendMessage: recipient = " + recipient); log.info("sendMessage: message = " + message); // ok = we sent to at least one device. boolean ok = false; // Send push message to phone C2DMessaging push = C2DMessaging.get(context); boolean res = false; String collapseKey = "" + message.hashCode(); // delete will fail if the pm is different than the one used to // load the object - we must close the object when we're done List<DeviceInfo> registrations = null; registrations = DeviceInfo.getDeviceInfoForUser(recipient); log.info("sendMessage: got " + registrations.size() + " registrations"); // Deal with upgrades and multi-device: // If user has one device with an old version and few new ones - // the old registration will be deleted. if (registrations.size() > 1) { // Make sure there is no 'bare' registration // Keys are sorted - check the first DeviceInfo first = registrations.get(0); Key oldKey = first.getKey(); if (oldKey.toString().indexOf("#") < 0) { // multiple devices, first is old-style. registrations.remove(0); // don't send to it pm.deletePersistent(first); } } int numSendAttempts = 0; for (DeviceInfo deviceInfo : registrations) { if (!"ac2dm".equals(deviceInfo.getType())) { continue; // user-specified device type } res = doSendViaC2dm(message, sender, push, collapseKey, deviceInfo); numSendAttempts++; if (res) { ok = true; } } if (ok) { return "Success: Message sent"; } else if (numSendAttempts == 0) { return "Failure: User " + recipient + " not registered"; } else { return "Failure: Unable to send message"; } } catch (Exception e) { return "Failure: Got exception " + e; } finally { pm.close(); } }