zdrqq 发表于 2013-2-3 10:39:10

iphone消息推送java后台证书生成及测试代码

iphone消息推送需要向APNS申请证书(如何申请,请参考:http://2015.iteye.com/blog/1337599),会生成三个文件:
1、Push.certSigningRequest
2、Push.p12
3、aps_developer_identity.cer
 
然后,java后台消息推送代码需要用到证书密码和路径,需要另外使用openssl生成,windows下的步骤如下:
打开openssl命令行:
 
1、将aps_developer_identity.cer转换成aps_developer_identity.pem格式
 输入命令: x509 -in aps_developer_identity.cer -inform DER -out aps_developer_identity.pem -outform PEM  
 
2、将p12格式的私钥转换成pem
 pkcs12 -nocerts -out Push_Noenc.pem -in Push.p12  
 
3、创建p12文件
  pkcs12 -export -in aps_developer_identity.pem -inkey Push_Noenc.pem -certfile Push.certSigningRequest -name "aps_developer_identity" -out aps_developer_identity.p12  
注意:-name 后面的名字 aps_developer_identity 与最后你生成的保持一致;

这样我们就得到了java等后台应用程序中使用的证书文件:aps_developer_identity.p12
 
如果linux下生成第三步报错,即可去掉  -certfile Push.certSigningRequest 生成(我是这样做的,也可以推送消息,去掉之前它老提示一个找不到cert的错误)
 
java 后台推送测试代码:(注意修改你的证书路径和密码,以及测试手机的token
 
/** * IPHONE推送 ** @param tokens *            设备ID列表 * @param message *            推送的消息 * @param badge *            iphone应用图标上小红圈上的数值 * @param sound *            消息提醒铃音 * @param certificatePath *            证书路径 * @param certificatePassword *            证书密码 * @param single *            true:单条 false:多条 */public void sendpush(List<String> tokens, String message, int badge,String sound, String certificatePath, String certificatePassword,boolean single) {try {logger.info("----------------message:" + message);PushNotificationPayload payLoad = new PushNotificationPayload();if(StringUtils.isNotEmpty(message)){payLoad.addAlert(message); // 消息内容}payLoad.addBadge(badge); // iphone应用图标上小红圈上的数值if (!StringUtils.isBlank(sound)) {payLoad.addSound(sound);// 铃音}PushNotificationManager pushManager = new PushNotificationManager();// true:表示的是产品发布推送服务 false:表示的是产品测试推送服务pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, certificatePassword, false));List<PushedNotification> notifications = new ArrayList<PushedNotification>();// 发送push消息if (single) {Device device = new BasicDevice();device.setToken(tokens.get(0));PushedNotification notification = pushManager.sendNotification(device, payLoad, true);notifications.add(notification);} else {List<Device> device = new ArrayList<Device>();for (String token : tokens) {device.add(new BasicDevice(token));}notifications = pushManager.sendNotifications(payLoad, device);}List<PushedNotification> failedNotifications = PushedNotification.findFailedNotifications(notifications);// List<PushedNotification> successfulNotifications =// PushedNotification// .findSuccessfulNotifications(notifications);if (failedNotifications != null) {int failed = failedNotifications.size();String errorLog = "失败条数=" + failed + "; ";for (PushedNotification failedNotification : failedNotifications) {Device d = failedNotification.getDevice();errorLog += "deviceId=" + d.getDeviceId() + "; token="+ d.getToken();}logger.info("消息推送失败记录:" + errorLog);}// int successful = successfulNotifications.size();} catch (Exception e) {logger.error("消息推送异常...", e);}}public static void main(String[] args) {String classpath = IphonePushService.class.getResource("/").getPath();PropertyConfigurator.configure(classpath+ "/config/properties/pushLog4j.properties");IphonePushService pushService = new IphonePushService();List<String> token = new ArrayList<String>();String device_token ="gt5a414g67a01183ef683r41e110ddrlof7d258h2c1678a99hj69586e589574";token.add(device_token);String certificatePath = "F://duanrong_zeng//temp//cert//java_dev_identity.p12";String certificatePassword = "123456";pushService.sendpush(token, null, 5, "", certificatePath,certificatePassword, true);} 使用的jar包,openssl见附件
本文参考了 http://2015.iteye.com/blog/1337599
 
 
页: [1]
查看完整版本: iphone消息推送java后台证书生成及测试代码