iphone 推送通知 java 实现
Push的原理:
从上图我们可以看到。
1、首先是应用程序注册消息推送。
2、 IOS跟APNS Server要deviceToken。应用程序接受deviceToken。
3、应用程序将deviceToken发送给PUSH服务端程序。
4、 服务端程序向APNS服务发送消息。
5、APNS服务将消息发送给iPhone应用程序。
这样我们就得到了在.net或java等后台应用程序中使用的证书文件:aps_developer_identity.p12
如果后台应用是php的话,那么可以按照 iOS消息推送机制中pem文件的生成这篇文章中的方法来生成php后台应用程序中使用的证书文件:ck.pem
前提准备,
在编写push notification之获取device token中拿到device token以后,需要把token字符串发送给应用的服务器端,即provider。
第三方依赖包(下载在下面):
bcprov-jdk16-145-1.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
log4j-1.2.16.jar
javapns-jdk16-163.jar
java代码:
package com.sdunisi.iphone.apns.send; import java.util.HashMap; import java.util.Iterator; import javapns.back.PushNotificationManager; import javapns.back.SSLConnectionHelper; import javapns.data.Device; import javapns.data.PayLoad; public class MainApnsSend { public static void main(String[] args) throws Exception { try { String deviceToken = "e775b5892f3334427c14def8aa4d8189a4ec1c795020072f4baa7ee92e50b1db";//iphone手机获取的token PayLoad payLoad = new PayLoad(); payLoad.addAlert("我的push测试");//push的内容 payLoad.addBadge(1);//图标小红圈的数值 payLoad.addSound("default");//铃音 PushNotificationManager pushManager = PushNotificationManager.getInstance(); pushManager.addDevice("iPhone", deviceToken); //Connect to APNs /************************************************ 测试的服务器地址:gateway.sandbox.push.apple.com /端口2195 产品推送服务器地址:gateway.push.apple.com / 2195 ***************************************************/ String host= "gateway.sandbox.push.apple.com"; int port = 2195; String certificatePath= "/Users/jcjc/Desktop/push_p.p12";//导出的证书 String certificatePassword= "sunlg";//此处注意导出的证书密码不能为空因为空密码会报错 pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12); //Send Push Device client = pushManager.getDevice("iPhone"); pushManager.sendNotification(client, payLoad); pushManager.stopConnection(); pushManager.removeDevice("iPhone"); } catch (Exception e) { e.printStackTrace(); } } }
整合后的代码
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()); } } }