java向IOS设备推送消息

java向IOS推送消息

1:所需要jar包

 javaPNS_2.2.jar, 

bcprov-jdk16-145-1.jar,

javapns-jdk16-163.jar

2:创建证书

需要开发者网站上导出的aps_developer_identity证书和Apple Development Push Services证书进行合成,
  生成可以供Java使用的p12证书。

3:部分代码如下:

//验证
	KeystoreManager.validateKeystoreParameter("证书路径");
	AppleNotificationServer server = new AppleNotificationServerBasicImpl("证书路径", "密码", "正式或测试(true and false) ");
	KeystoreManager.verifyKeystoreContent(server, keystore);
	//推送
	PushNotificationPayload complexPayload = PushNotificationPayload.complex();
	ArrayList<String> parameters = new ArrayList<String>();
	complexPayload.addCustomAlertBody("附件");
	complexPayload.addCustomAlertActionLocKey("Open AppMail");
	complexPayload.addCustomAlertLocKey("%@[%@]%@");
	parameters.add("消息来源");
	parameters.add("标题");
	parameters.add("消息内容");
	complexPayload.addCustomAlertLocArgs(parameters);
	complexPayload.addBadge(1);
	complexPayload.addSound("default");//铃声默认
	complexPayload.addCustomDictionary("type", "0");


	List<String> tokens = new ArrayList<String>();
	tokens.add("81d3788f2b3339990d9c3b1215a67a70975591304a20665e927668b282e6c782");//从苹果设备获得设备id
	List<PushedNotification> notifications = Push.payload(complexPayload, "证书路径", "密码", "正式或测试(true and false) ", tokens);
	
	//验证推送是否成功
	for (PushedNotification notification : notifications) {
		Log.info(Log.SYS, notification.toString());
		if (notification.isSuccessful()) {
			Log.info(Log.PUSH, "succ|" + notification.getDevice().getToken());
		} else {
			Log.info(Log.PUSH, "fail|" + notification.getDevice().getToken() + "|" + notification.getResponse().getMessage());
		}
	}


 

你可能感兴趣的:(java,ISO,推送消息)