if (entry.getValue() != null) {
messageBuilder.addData(entry.getKey(), entry.getValue());
}
}
Message message = messageBuilder.build();
PushResponse pushResponse = new PushResponse();
try {
MulticastResult multicastResult = sender.send(message, Lists.newArrayList(registrationIds), 10);
// analyze the results
for (int i = 0; i < registrationIds.size(); i++) {
Result result = multicastResult.getResults().get(i);
Device device = devices.get(i);
if (result.getMessageId() != null) {
LOGGER.info("Sent GCM message to " + device);
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration id: update it
device.updateRegistrationId(canonicalRegId);
pushResponse.addDeviceToUpdate(device);
LOGGER.info("Updated registration id of device " + device);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// Application has been removed from device - unregister it
pushResponse.addDeviceToRemove(device);
LOGGER.info("Unregistered device " + device);
} else {
LOGGER.info("Error [" + error + "] when sending message to device " + device);
}
}