首页 > 代码库 > 【iOS】push通知(JavaEE实现)

【iOS】push通知(JavaEE实现)

第一次做iOS push,回想起来还挺简单的。
因为做的时候没留下图,所以就不上图了

去http://developer.apple.com ,用付过费的开发者帐号,创建一个新的应用。勾选上Enable Push Notification;
然后在本机的Key Chain 中请求从证书颁发机构请求一个证书。 上传, 下载这一套流程我就不说,网上很多

主要介绍一下psuh的详细过程

证书什么的, 客户端,我都已经配置好了。
可服务端的人没做过这个。没办法,我来封装一下服务端的代码吧。

我选择了Java Apns

先上一段我封装好的JavaEE的代码

public interface APNSFeedBack {public void APNSFeedBack(Boolean success,Exception exception);}

 

public class ApplePush extends Thread {private PushNotificationManager pushNotificationManager;private AppleNotificationServer appleNotificationServer;private ArrayList<Device> devices;private Payload payload;private APNSFeedBack feedBack;static public void main(String argc[]){try {String tokenDevice = "0b307cfe5d55a3018f281d68afb7ae188c102628b441587e526707fd41e62125";PushNotificationPayload payload = new PushNotificationPayload();payload.addAlert("大家好啊");ArrayList<String> devicesString = new ArrayList<String>();devicesString.add(tokenDevice);ApplePush push = new ApplePush();push.sendNotification(devicesString, payload, new APNSFeedBack() {@Overridepublic void APNSFeedBack(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(ArrayList<String>devicesString,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("设备惟一标识异常"));}//构建Devicethis.devices = new ArrayList<Device>(devicesString.size());for (int i = 0 ; i < devicesString.size() ; i ++){String deviceString = devicesString.get(i);Device device = new BasicDevice();device.setToken(deviceString);this.devices.add(device);}//保存句柄this.payload = payload;this.feedBack = feedBack;//push管理程序this.pushNotificationManager = new PushNotificationManager();//初始化证书和端口this.appleNotificationServer = new AppleNotificationServerBasicImpl(p12Path,password,!isDeveloper);this.start();}catch (Exception ex){System.out.print("抛出异常");ex.printStackTrace();feedBack.APNSFeedBack(false,ex);}return true;}@Overridepublic void run() {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);}}}

 

【iOS】push通知(JavaEE实现)