certificatePath = "zn_server.p12";
production = false;
certificatePassword = "123456";
if(ansBasicImpl == null && StringUtils.isNotBlank(certificatePath)){
try {
String fullPath = PushMessageServiceImpl.class.getClassLoader().getResource(certificatePath).getPath();
ansBasicImpl = new AppleNotificationServerBasicImpl(fullPath, certificatePassword, production);
// 与apple建立连接
pushManager.initializeConnection(ansBasicImpl);
logger.info("与apple建立连接1成功");
} catch (Exception e) {
e.printStackTrace();
logger.info("Ios doInitializeConnection: txnId= Ios execute :" + e.getMessage());
logger.info("=AppleApnsSAO:与apple建立连接1异常! "+e);
ansBasicImpl = null;
}
}
}
public void push2IOS(String token, String message, Map<String, String> customParam){
try {
// 普通定制参数
PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert(message); //提醒文本
payload.addSound("default"); //提醒铃声
payload.addBadge(1);
this.addCustomDictionary(payload, customParam);
Device device = new BasicDevice(token);
// 发送信息到apple服务器
PushedNotification notification = pushManager.sendNotification(device, payload, false); // 发送后不要关闭连接
List<PushedNotification> notifications = new ArrayList<PushedNotification>();
if(notification!=null){
notifications.add(notification);
}
doAppleResponse(notifications);
} catch (Exception e) {
logger.error(e);
logger.error("error tokenID = "+token);
}
}
public void push2IOS2(String deviceToken, String message, Map<String, String> customParam){
javapns.back.PushNotificationManager pushManager2 = null;
String deviceId = null;
certificatePath = "zn_server.p12";
production = false;
certificatePassword = "123456";
try
{
String fullPath = PushMessageServiceImpl.class.getClassLoader().getResource(certificatePath).getPath();
PayLoad payLoad = new PayLoad();
payLoad.addAlert(message); //提醒文本
payLoad.addSound("default"); //提醒铃声
payLoad.addBadge(1);
this.addCustomDictionary2(payLoad, customParam);
deviceId = String.valueOf(payLoad.hashCode());
logger.info("Send notification for '" + deviceToken + ". PayLoad(" + payLoad.getPayloadAsBytes().length + " bytes) is:\n" + payLoad);
pushManager2 = javapns.back.PushNotificationManager.getInstance();
pushManager2.addDevice(deviceId, deviceToken);
logger.info("Add divice (id='" + deviceId + "').");
logger.info("Add divice (id='" + deviceId + "').");
pushManager2.initializeConnection("gateway.sandbox.push.apple.com", 2195, fullPath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
logger.info("initializeConnection='" + "gateway.sandbox.push.apple.com" +" .. "+ 2195 +" .. "+fullPath +" .. "+certificatePassword + " ..");
javapns.data.Device client = pushManager2.getDevice(deviceId);
logger.info("Before send to " + deviceId + ".");
pushManager2.sendNotification(client, payLoad);
logger.info("Sended to " + deviceId + ".");
}catch(Exception e){
logger.error(e);
}
finally
{
try
{
pushManager2.stopConnection();
}
catch(Exception e)
{
logger.error("StopConnectionException", e);
}
try
{
pushManager2.removeDevice(deviceId);
}
catch(Exception e)
{
logger.error("RemoveDeviceException", e);
}
}
}
private Map<String, Boolean> doAppleResponse(List<PushedNotification> notifications){
List<PushedNotification> failedNotifications = PushedNotification.findFailedNotifications(notifications); // 失败通知
List<PushedNotification> successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications); // 成功通知
int failed = failedNotifications.size();
int successful = successfulNotifications.size();
String result = (failed==0 ? "全部成功": (successful>0 ? "部分成功" : "全部失败") );
logger.info("doAppleResponse 结果: "+result+" failed :"+failed +"; successful:" + successful);
Map<String, Boolean> resultMap = new HashMap<String, Boolean>();
for(PushedNotification successfulNotification : successfulNotifications){
resultMap.put(successfulNotification.getDevice().getToken(), Boolean.TRUE);
logger.info("success Token:" + successfulNotification.getDevice().getToken()+" detail: "+successfulNotification);
logger.info("success notification detail: "+successfulNotification);
}
for(PushedNotification failedNotification : failedNotifications){
resultMap.put(failedNotification.getDevice().getToken(), Boolean.FALSE);
logger.info("failed Token:" + JSON.toJSONString(failedNotification.getDevice()));
logger.info("failed Exception:" + failedNotification.getException());
logger.info("failed LatestTransmissionAttempt:" + failedNotification.getLatestTransmissionAttempt());
}
logger.info("doAppleResponse resultMap: "+resultMap);
return resultMap;
}
@Override
public void doClose() throws Exception {
if(ansBasicImpl != null) {
try {
pushManager.stopConnection(); // 关闭与apple建立的连接
ansBasicImpl = null;
logger.info("=AppleApnsSAO:关闭与apple建立的连接成功");
} catch (Exception e) {
e.printStackTrace();
logger.error("Ios doClose =" +"Ios doClose :" + e);
} finally {
// 记录断开苹果服务器接口日志
}
}
}