---
title: "iphone推送 java代码实现"
author: "erwin_king"
date: "2011-11-07T18:32:36Z"
tags: []
categories: ["移动开发"]
slug: "iphone-java"
draft: false
lastmod: "2011-11-07T18:32:36Z"
id: 1240602
url: "https://erwin-king.iteye.com/blog/1240602"
---

最近在研究iphone推送的java实现，看过发现原来很简单，以下是我根据网上源码修改的程序，具体里面的证书和手机token的生产就不解释了，本人没有搞过iphone，有需要的可以再网上搜索以下，很多也很简单。

public class PushUtils {  
 private static Logger logger = LoggerFactory.getLogger(PushUtils.class);

 /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
 测试推送服务器地址：gateway.sandbox.push.apple.com /2195  
 产品推送服务器地址：gateway.push.apple.com / 2195  
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
 private static String host = "gateway.sandbox.push.apple.com";  
 private static int port = 2195;

 public static void main(String\[] args) throws Exception {

  String deviceToken = "f2a070af d6fc27ca c1844810 f6904fcd b28e6fc7 1d207d63 5a01f1af e0850f0a";//iphone手机获取的token  
  String deviceToken2 = "0c0028e4 ca4049d6 52dfdafb c4b37c25 2c0386aa d14545eb f3859b56 d5593c23";//iphone手机获取的token  
  List&lt;String&gt; deviceTokens = new ArrayList&lt;String&gt;();  
  deviceTokens.add(deviceToken);  
  deviceTokens.add(deviceToken2);

  String content = "此次升级更新的东西";//push的内容  
  String p12File = "d:/push2.p12";//这里是一个.p12格式的文件路径，需要去apple官网申请一个    
  String p12FilePassword = "wiscom";//此处注意导出的证书密码不能为空因为空密码会报错  
  push2More(p12File, p12FilePassword, deviceTokens);//群组推送  
  push2One(p12File, p12FilePassword, deviceToken2);//单个推送

 }

 /\**  
  * 向单个iphone手机推送消息.  
  * @param deviceToken iphone手机获取的token  
  * @param p12File .p12格式的文件路径  
  * @param p12Pass .p12格式的文件密码  
  * @param customDictionarys CustomDictionary字典组  
  * @param content 推送内容  
  \*/  
 public static void push2One(String p12File, String p12Pass, String deviceToken, String content) {  
  try {  
   PayLoad payLoad = new PayLoad();  
   payLoad.addAlert(content);//push的内容  
   payLoad.addBadge(1);//应用图标上小红圈上的数值  
   payLoad.addSound("default");//铃音

   //添加字典  
   payLoad.addCustomDictionary("url", "[www.baidu.com](http://www.baidu.com)");  
   PushNotificationManager pushManager = PushNotificationManager.getInstance();  
   pushManager.addDevice("iphone", deviceToken);

   //链接到APNs  
   pushManager.initializeConnection(host, port, p12File, p12Pass, SSLConnectionHelper.KEYSTORE\_TYPE\_PKCS12);

   //开始推送  
   Device client = pushManager.getDevice("iphone");  
   pushManager.sendNotification(client, payLoad);  
   //断开链接  
   pushManager.stopConnection();  
   pushManager.removeDevice("iphone");  
   logger.info("iphone 推送消息成功");  
  } catch (Exception e) {  
   //   System.out.println("iphone 推送消息异常：" + e.getMessage());  
   logger.error("iphone 推送消息异常：" + e.getMessage());  
  }  
 }

 /\**  
  * 向iphone群组推送消息.  
  * @param deviceTokens iphone手机获取的token  
  * @param p12File .p12格式的文件路径  
  * @param p12Pass .p12格式的文件密码  
  * @param customDictionarys CustomDictionary字典  
  * @param content 推送内容  
  \*/  
 public static void push2More(String p12File, String p12Pass, List&lt;String&gt; deviceTokens, String content) {  
  try {  
   PayLoad payLoad = new PayLoad();  
   payLoad.addAlert(content);//push的内容  
   payLoad.addBadge(1);//应用图标上小红圈上的数值  
   payLoad.addSound("default");//铃音

   //添加字典  
    payLoad.addCustomDictionary("url", "[www.baidu.com](http://www.baidu.com)");  
   PushNotificationManager pushManager = PushNotificationManager.getInstance();

   //链接到APNs  
   pushManager.initializeConnection(host, port, p12File, p12Pass, SSLConnectionHelper.KEYSTORE\_TYPE\_PKCS12);

   //开始循环推送  
   for (int i = 0; i &lt; deviceTokens.size(); i++) {  
    pushManager.addDevice("iphone" + i, deviceTokens.get(i));  
    Device client = pushManager.getDevice("iphone" + i);  
    pushManager.sendNotification(client, payLoad);  
   }  
   //断开链接  
   pushManager.stopConnection();  
   for (int i = 0; i &lt; deviceTokens.size(); i++) {  
    pushManager.removeDevice("iphone" + i);  
   }  
   logger.info("iphone 推送消息成功");  
   //   System.out.println("iphone 推送消息成功");  
  } catch (Exception e) {  
   logger.error("iphone 推送消息异常：" + e.getMessage());  
   //   System.out.println("iphone 推送消息异常：" + e.getMessage());  
  }  
 }  
}