// no need to send empty list
if (tokens.isEmpty()) {
return;
}
final iOSVariant iOSVariant = (iOSVariant) variant;
PayloadBuilder builder = APNS.newPayload()
// adding recognized key values
.alertBody(pushMessage.getAlert()) // alert dialog, in iOS
.badge(pushMessage.getBadge()) // little badge icon update;
.sound(pushMessage.getSound()) // sound to be played by app
.category(pushMessage.getActionCategory()); // iOS8: User Action category
// apply the 'content-available:1' value:
if (pushMessage.isContentAvailable()) {
// content-available is for 'silent' notifications and Newsstand
builder = builder.instantDeliveryOrSilentNotification();
}
builder = builder.customFields(pushMessage.getData()); // adding other (submitted) fields
// we are done with adding values here, before building let's check if the msg is too long
if (builder.isTooLong()) {
logger.log(Level.WARNING, "Nothing sent to APNs since the payload is too large");
// invoke the error callback and return, as it is pointless to send something out
callback.onError("message too long for APNs");
return;
}
// 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);
callback.onError("Error sending messages to APN server");
} finally {