最近在研究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<String> deviceTokens = new ArrayList<String>();
??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");
???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<String> deviceTokens, String content) {
??try {
???PayLoad payLoad = new PayLoad();
???payLoad.addAlert(content);//push的内容
???payLoad.addBadge(1);//应用图标上小红圈上的数值
???payLoad.addSound("default");//铃音
???//添加字典
????payLoad.addCustomDictionary("url", "www.baidu.com");
???PushNotificationManager pushManager = PushNotificationManager.getInstance();
???//链接到APNs
???pushManager.initializeConnection(host, port, p12File, p12Pass, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
???//开始循环推送
???for (int i = 0; i < 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 < 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());
??}
?}
}
应该就是给多个iphone客户端push消息吧,我看他就是把deviceTokens循环了一下