不废话,直接来思路(亲测):
第一步导入jar包:(开源项目Pushy)
<dependency>
<groupId>com.turo</groupId>
<artifactId>pushy</artifactId>
<version>0.10.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.alpn</groupId>
<artifactId>alpn-api</artifactId>
<version>1.1.2.v20150522</version>
</dependency>
<!-- <dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>2.0.6.Final</version>
</dependency>
--> <!-- 其他工具 -->
细节点(花费3h调试):
如果tomcat里面含有tcnative,就不需要netty-tcnative,避免冲突;
如果tomcat里面没有tcnative, 就需要引入tcnative;
第二步代码演示:
Future<PushNotificationResponse<SimpleApnsPushNotification>> responseFuture = null;
try {
// 创建ApnsClient
final ApnsClient apnsClient = new ApnsClientBuilder().setClientCredentials(new File(p12证书), p12密码).build();
// 沙箱环境
final Future<Void> connectFutrue = apnsClient.connect(ApnsClient.DEVELOPMENT_APNS_HOST);
// 正式环境
// final Future<Void> connectFutrue = apnsClient.connect(ApnsClient.PRODUCTION_APNS_HOST);
connectFutrue.await();
final ApnsPayloadBuilder payBuilder = new ApnsPayloadBuilder();
// 通知信息
payBuilder.setAlertTitle("Title");
payBuilder.setAlertBody("AlertBody");
payBuilder.setSoundFileName(ApnsPayloadBuilder.DEFAULT_SOUND_FILENAME);
payBuilder.addCustomProperty("image", "https://onevcat.com/assets/images/background-cover.jpg");
payBuilder.setMutableContent(true);
payBuilder.setBadgeNumber(1);
// 通知的最大长度
String payload = payBuilder.buildWithDefaultMaximumLength();
// 获取APNS指定的token
final String token = TokenUtil.sanitizeTokenString(device.getDeviceToken());
SimpleApnsPushNotification simpleApnsPushNotification = new SimpleApnsPushNotification(token, device.getEdition().getBundleId(), payload);
responseFuture = apnsClient.sendNotification(simpleApnsPushNotification);
responseFuture.addListener(new GenericFutureListener<Future<PushNotificationResponse<SimpleApnsPushNotification>>>() {
@Override
public void operationComplete(Future<PushNotificationResponse<SimpleApnsPushNotification>> arg0)
throws Exception {
try {
final PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse = arg0.get();
if (pushNotificationResponse.isAccepted()) {
System.out.println("Push notification accepted by APNs gateway.");
} else {
System.out.println("Notification rejected by the APNs gateway: " + pushNotificationResponse.getRejectionReason());
if (pushNotificationResponse.getTokenInvalidationTimestamp() != null) {
System.out.println("\t…and the token is invalid as of " + pushNotificationResponse.getTokenInvalidationTimestamp());
}
}
} catch (final ExecutionException e) {
System.err.println("Failed to send push notification.");
e.printStackTrace();
if (e.getCause() instanceof ClientNotConnectedException) {
System.out.println("Waiting for client to reconnect…");
apnsClient.getReconnectionFuture().await();
System.out.println("Reconnected.");
}
}
}
});
}
}
// 结束后关闭连接, 该操作会直到所有notification都发送完毕并回复状态后关闭连接
final Future<Void> disconnectFuture = apnsClient.disconnect();
disconnectFuture.await();
return responseFuture;
} catch (Exception e) {
if(e instanceof InterruptedException) {
System.out.println("Failed to disconnect APNs , timeout");
}
e.printStackTrace();
}
}
更多文章,请关注:http://blog.csdn.net/qq_37022150