不廢話,直接來思路(親測):
第一步導入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