import io.netty.channel.nio.NioEventLoopGroup;
public class IphonePush extends HttpServlet {
private static ApnsClient apnsClient = null;
@Override
@RequestMapping("/iphonepush")
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
MyLogUtil.log("push start!", "iphonepush.log");
SendMsgToAPNs();
MyLogUtil.log("create client success!", "iphonepush.log");
response.getWriter().write("success");
}
public void SendMsgToAPNs() {
try {
String deviceToken = "e02ee1c97c2a78d2d3fcccd271cc067ba6005b0b2dd2bda66ce7b555fae18a29";
String topic = "com.phoneEncryption";
String payload = "{ \"aps\" : {\"alert\" : \"测试\", \"sound\" : \"default\", \"badge\" :1},\"cuiyaonan2000\":\"cuiyaonan2000\" }";
Date invalidationTime = new Date(System.currentTimeMillis() + 60 * 60 * 1000L);
Instant date = invalidationTime.toInstant();
DeliveryPriority priority = DeliveryPriority.IMMEDIATE;
PushType pushType = PushType.VOIP;
String collapseId = UUID.randomUUID().toString();
UUID apnsId = UUID.randomUUID();
SimpleApnsPushNotification msg = new SimpleApnsPushNotification(deviceToken, topic, payload, date,
priority, pushType, collapseId, apnsId);
PushNotificationFuture<SimpleApnsPushNotification, PushNotificationResponse<SimpleApnsPushNotification>> future = getAPNSConnect()
.sendNotification(msg);
MyLogUtil.log("push succeed3", "iphonepush.log");
PushNotificationResponse<SimpleApnsPushNotification> response = future.get();
MyLogUtil.log("------------->" + response, "iphonepush.log");
}
catch (Exception e) {
e.printStackTrace();
String str = e.getMessage() + "failed";
MyLogUtil.log(str, "iphonepush.log");
return;
}
MyLogUtil.log("push succeed", "iphonepush.log");
}
public static ApnsClient getAPNSConnect() {
if (apnsClient == null) {
try {
MyLogUtil.log("push succeed4 get connect apns", "iphonepush.log");
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
apnsClient = new ApnsClientBuilder().setApnsServer(SANDBOX_APNS_HOST, 443)//ApnsClientBuilder.DEVELOPMENT_APNS_HOST
.setClientCredentials(new File("/root/work/Alipay/push4.p12"), "123qweasd")
.setConcurrentConnections(4).setEventLoopGroup(eventLoopGroup).build();
MyLogUtil.log("push succeed4 create apns client success", "iphonepush.log");
// EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
//
} catch (Exception e) {
MyLogUtil.log("ios get pushy apns client failed!", "iphonepush.log");
e.printStackTrace();
}
}
return apnsClient;
}
}