private ApnsService buildApnsService(iOSVariant iOSVariant, final NotificationSenderCallback notificationSenderCallback) {
// this check should not be needed, but you never know:
if (iOSVariant.getCertificate() != null && iOSVariant.getPassphrase() != null) {
final ApnsServiceBuilder builder = APNS.newService().withNoErrorDetection();
// using the APNS Delegate callback to trigger our own notifications for success/failure status:
builder.withDelegate(new ApnsDelegateAdapter() {
@Override
public void messageSent(ApnsNotification message, boolean resent) {
notificationSenderCallback.onSuccess();
}
@Override
public void messageSendFailed(ApnsNotification message, Throwable e) {
logger.log(Level.SEVERE, "Error sending payload to APNs server", e);
notificationSenderCallback.onError("Error sending payload to APNs server");
}
});
// add the certificate:
try {
ByteArrayInputStream stream = new ByteArrayInputStream(iOSVariant.getCertificate());
builder.withCert(stream, iOSVariant.getPassphrase());
// release the stream
stream.close();
} catch (Exception e) {
logger.log(Level.SEVERE, "Error reading certificate", e);
// indicating an incomplete service
return null;
}
// pick the destination:
if (iOSVariant.isProduction()) {
builder.withProductionDestination();
} else {
builder.withSandboxDestination();
}
// create the service
return builder.build();
}
// null if, why ever, there was no cert/passphrase
return null;
}