use of com.google.android.gcm.server.Result in project convertigo by convertigo.
the class PushNotificationStep method PushToGCM.
protected void PushToGCM(Context javascriptContext, Scriptable scope, List<Parameters> dictionary) throws EngineException, Exception {
Engine.logBeans.debug("Push notification, Notifying Android devices");
try {
if (dictionary == null) {
Engine.logBeans.debug("Push notification, dictionary empty");
return;
}
evaluate(javascriptContext, scope, this.GCMApiKey, "gcmapikey", false);
sGCMApiKey = evaluated instanceof Undefined ? "" : evaluated.toString();
Sender sender = new Sender(sGCMApiKey);
// get Token List
StepSource tokens = getTokenSource();
NodeList list;
list = tokens.inError() ? null : tokens.getContextOutputNodes();
if (list != null) {
ArrayList<String> devicesList = new ArrayList<String>();
for (int i = 0; i < list.getLength(); i++) {
String token = getNodeValue(list.item(i));
if (token.startsWith("gcm:")) {
devicesList.add(token.substring(4));
Engine.logBeans.trace("Push notification, Android device " + token.substring(4) + " will be notified");
}
}
if (devicesList.isEmpty()) {
Engine.logBeans.debug("Push notification, device list empty");
return;
}
// use this line to send message with payload data
Message.Builder builder = new Message.Builder().collapseKey("1").timeToLive(AndroidTimeToLive).delayWhileIdle(true);
// add all dictionary entries in turn
for (int i = 0; i < dictionary.size(); i++) {
// if plugin specified, check it and skip accordingly
if ((dictionary.get(i).plug.length() != 0) && !(dictionary.get(i).plug.equalsIgnoreCase("gcm") || dictionary.get(i).plug.equalsIgnoreCase("all")))
continue;
// if only one dictionary entry, hardcode key as "message"
if (dictionary.size() == 1) {
builder.addData("message", dictionary.get(i).value);
break;
}
builder.addData(dictionary.get(i).name, dictionary.get(i).value);
}
Message message = builder.build();
// Use this for multicast messages
MulticastResult multicastResult;
try {
multicastResult = sender.send(message, devicesList, NB_RETRIES);
} catch (IOException e) {
errorMessage = "Push notification, error posting Android messages " + e.toString();
Engine.logBeans.debug(errorMessage);
return;
}
if (multicastResult.getResults() != null) {
Engine.logBeans.debug("Push notification, Android devices notified: " + multicastResult.toString());
List<Result> results = multicastResult.getResults();
for (int i = 0; i < devicesList.size(); i++) {
Result result = results.get(i);
String regId = devicesList.get(i);
String messageId = result.getMessageId();
String canonicalRegId = result.getCanonicalRegistrationId();
if (messageId != null) {
Engine.logBeans.info("Push notification, succesfully sent message to device: " + regId + "; messageId = " + messageId);
canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
saveErrorForOutput("gcm", regId, messageId, canonicalRegId, "Device registered more than once");
// same device has more than on registration id: update it
Engine.logBeans.info("Push notification, warning, same device has more than on registration canonicalRegId " + canonicalRegId);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(com.google.android.gcm.server.Constants.ERROR_NOT_REGISTERED)) {
saveErrorForOutput("gcm", regId, messageId, canonicalRegId, "Application removed from device");
// application has been removed from device - unregister it
Engine.logBeans.info("Push notification, unregistered device: " + regId);
} else {
saveErrorForOutput("gcm", regId, "", canonicalRegId, error);
Engine.logBeans.debug("Push notification, error sending message to '" + regId + "': " + error);
}
}
}
} else {
errorMessage = "Push notification, Android device error: " + multicastResult.getFailure();
Engine.logBeans.error(errorMessage);
}
}
} catch (Exception e) {
errorMessage = "Push notification, Android device exception: " + e;
Engine.logBeans.error(errorMessage);
}
}
Aggregations