public class ApplePush extendsThread {privatePushNotificationManager pushNotificationManager;privateAppleNotificationServer appleNotificationServer;private ArrayListdevices;privatePayload payload;privateAPNSFeedBack feedBack;static public voidmain(String argc[])
{try{
String tokenDevice= "0b307cfe5d55a3018f281d68afb7ae188c102628b441587e526707fd41e62125";
PushNotificationPayload payload= newPushNotificationPayload();
payload.addAlert("大家好啊");
ArrayList devicesString = new ArrayList();
devicesString.add(tokenDevice);
ApplePush push= newApplePush();
push.sendNotification(devicesString, payload,newAPNSFeedBack() {
@Overridepublic voidAPNSFeedBack(Boolean success, Exception exception) {if(success) {
System.out.println("请求成功");
}else{
System.out.println("请求失败 exception = " +exception);
}
}
});
System.out.println("正在push中…");
}catch(Exception exception)
{
System.out.println("Json库加载失败");
}
}public Boolean sendNotification(ArrayListdevicesString,PushNotificationPayload payload,APNSFeedBack feedBack)
{//初始化
try{
String p12Path= "/Users/WangHeShiDai/Desktop/PushKey.p12"; //证书路径
String password = "wangheshidai"; //密码
Boolean isDeveloper = true; //是不是开发模式
if (devicesString == null || devicesString.size() == 0)
{
feedBack.APNSFeedBack(false,new Exception("设备惟一标识异常"));
}//构建Device
this.devices = new ArrayList(devicesString.size());for (int i = 0 ; i < devicesString.size() ; i ++)
{
String deviceString=devicesString.get(i);
Device device= newBasicDevice();
device.setToken(deviceString);this.devices.add(device);
}//保存句柄
this.payload =payload;this.feedBack =feedBack;//push管理程序
this.pushNotificationManager = newPushNotificationManager();//初始化证书和端口
this.appleNotificationServer = new AppleNotificationServerBasicImpl(p12Path,password,!isDeveloper);this.start();
}catch(Exception ex)
{
System.out.print("抛出异常");
ex.printStackTrace();
feedBack.APNSFeedBack(false,ex);
}return true;
}
@Overridepublic voidrun() {try{//连接苹果服务器
this.pushNotificationManager.initializeConnection(this.appleNotificationServer);//发送消息
this.pushNotificationManager.sendNotifications(this.payload, this.devices);//断开链接
this.pushNotificationManager.stopConnection();
System.out.println("发送成功");this.feedBack.APNSFeedBack(true,null);
}catch(Exception exception)
{
System.out.print("抛出异常");
exception.printStackTrace();this.feedBack.APNSFeedBack(false,exception);
}
}
}