<properties>
<pushy.version>0.12.1</pushy.version>
</properties>
<dependency>
<groupId>com.turo</groupId>
<artifactId>pushy</artifactId>
<version>${pushy.version}</version>
</dependency>
private static final Map<Boolean,ApnsClient> apnsClientMapCache = new HashMap<>();
@Value("${is_dev_mode:0}")
Integer isDevMode;
@Value("${ios_push_file_path}")
String iosPushFilePath;
@Value("${ios_push_key}")
String iosPushKey;
@Value("${ios_push_bundle_id}")
String iosPushBundleId;
public ApnsClient getAPNSConnect() {
boolean isDebug=isDevMode.intValue()==1;
log.info("isDebug?"+isDebug);
ApnsClient apnsClient = apnsClientMapCache.get(isDebug);
if (apnsClient == null) {
try {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
apnsClient = new ApnsClientBuilder().setApnsServer(isDebug?ApnsClientBuilder.DEVELOPMENT_APNS_HOST:ApnsClientBuilder.PRODUCTION_APNS_HOST)
.setClientCredentials(new File(iosPushFilePath), iosPushKey)
.setConcurrentConnections(4).setEventLoopGroup(eventLoopGroup).build();
apnsClientMapCache.put(isDebug,apnsClient);
} catch (Exception e) {
log.error("ios get pushy apns client failed!");
e.printStackTrace();
}
}
return apnsClient;
}
public String applePush(final String deviceToken, String alertTitle, String alertBody, boolean contentAvailable, JSONObject customProperty) {
ApnsClient apnsClient = getAPNSConnect();
ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();
if (alertBody != null && alertTitle != null) {
payloadBuilder.setAlertBody(alertBody);
payloadBuilder.setAlertTitle(alertTitle);
}
if (customProperty != null) {
int badge = ValueUtil.getInt(customProperty.get("badge"));
if (badge > 0) {
payloadBuilder.setBadgeNumber(badge);
}
for (Map.Entry<String, Object> map : customProperty.entrySet()) {
payloadBuilder.addCustomProperty(map.getKey(), map.getValue());
}
}
payloadBuilder.setContentAvailable(contentAvailable);
String payload = payloadBuilder.buildWithDefaultMaximumLength();
final String token = TokenUtil.sanitizeTokenString(deviceToken);
String topic = iosPushBundleId;
SimpleApnsPushNotification pushNotification = new SimpleApnsPushNotification(token, topic, payload);
final Future<PushNotificationResponse<SimpleApnsPushNotification>> future = apnsClient.sendNotification(pushNotification);
future.addListener(new GenericFutureListener<Future<PushNotificationResponse>>() {
@Override
public void operationComplete(Future<PushNotificationResponse> pushNotificationResponseFuture) throws Exception {
if (future.isSuccess()) {
final PushNotificationResponse<SimpleApnsPushNotification> response = future.getNow();
if (response.isAccepted()) {
logger.info("send notification device is success ");
} else {
Date invalidTime = response.getTokenInvalidationTimestamp();
log.error("Notification rejected by the APNs gateway: " + response.getRejectionReason());
if (invalidTime != null) {
log.error("\t…and the token is invalid as of " + response.getTokenInvalidationTimestamp());
}
}
} else {
logger.error("send notification device is failed {} ", future.cause().getMessage());
}
}
});
return "success";
}