}
// all good, let's build the JSON payload for APNs
final String apnsMessage = builder.build();
ApnsService service = buildApnsService(iOSVariant, callback);
if (service != null) {
try {
logger.log(Level.FINE, "Sending transformed APNs payload: " + apnsMessage);
// send:
service.start();
Date expireDate = createFutureDateBasedOnTTL(pushMessage.getTimeToLive());
service.push(tokens, apnsMessage, expireDate);
logger.log(Level.INFO, "Message to APNs has been submitted");
// after sending, let's ask for the inactive tokens:
final Set<String> inactiveTokens = service.getInactiveDevices().keySet();
// transform the tokens to be all lower-case:
final Set<String> transformedTokens = lowerCaseAllTokens(inactiveTokens);
// trigger asynchronous deletion:
if (! transformedTokens.isEmpty()) {
logger.log(Level.INFO, "Deleting '" + inactiveTokens.size() + "' invalid iOS installations");
clientInstallationService.removeInstallationsForVariantByDeviceTokens(iOSVariant.getVariantID(), transformedTokens);
}
} catch (RuntimeException e) {
logger.log(Level.SEVERE, "Error sending messages to APN server", e);
} finally {
// tear down and release resources:
service.stop();
}
} else {
logger.log(Level.SEVERE, "No certificate was found. Could not send messages to APNs");
callback.onError("No certificate for APNs was found");
}